Given an integer N and an array arr[], the task is to check if a sequence of first N natural numbers, i.e. {1, 2, 3, .. N} can be made equal to arr[] by choosing any pair (i, j) from the sequence and replacing both i and j by GCD of i and j. If possible, then print “Yes”. Otherwise, print “No”.
Examples:
Input: N = 4, arr[] = {1, 2, 3, 2}
Output: Yes
Explanation: For the pair (2, 4) in the sequence {1, 2, 3, 4}, GCD(2, 4) = 2. Now, the sequence modifies to {1, 2, 3, 2}, which is same as arr[].Input: N = 3, arr[] = {1, 2, 2}
Output: No
Approach: The idea is based on the fact that the GCD of two numbers lies between 1 and the minimum of the two numbers. By definition of gcd, it’s the greatest number that divides both. Therefore, make the number at an index smaller if and only if there exists some number which is its factor. Hence, it can be concluded that for every ith index in the array, if the follow condition holds true, the array arr[] can be obtained from the sequence of first N natural numbers.
(i + 1) % arr[i] == 0
Follow the steps below to solve the problem:
- Traverse the array arr[] using variable i.
- For every ith index, check if (i + 1) % arr[i] is equal to 0 or not. If found to be false for any array element, print “No”.
- Otherwise, after complete traversal of the array, print “Yes”.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to check if array arr[] // can be obtained from first N // natural numbers or not void isSequenceValid(vector<int>& B, int N) { for (int i = 0; i < N; i++) { if ((i + 1) % B[i] != 0) { cout << "No"; return; } } cout << "Yes"; } // Driver Code int main() { int N = 4; vector<int> arr{ 1, 2, 3, 2 }; // Function Call isSequenceValid(arr, N); return 0; } |
Yes
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum
- Maximum GCD among all pairs (i, j) of first N natural numbers
- Maximize modulus by replacing adjacent pairs with their modulus for any permutation of given Array
- Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum
- Split N natural numbers into two sets having GCD of their sums greater than 1
- Maximize array sum by replacing equal adjacent pairs by their sum and X respectively
- Smallest array that can be obtained by replacing adjacent pairs with their products
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- Minimize remaining array element by repeatedly replacing pairs by half of one more than their sum
- Reduce array to a single element by repeatedly replacing adjacent unequal pairs with their maximum
- Minimize array sum by replacing greater and smaller elements of pairs by half and double of their values respectively atmost K times
- Count pairs of natural numbers with GCD equal to given number
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Maximum possible GCD after replacing at most one element in the given array
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M
- Check if product of first N natural numbers is divisible by their sum
- Split first N natural numbers into two sets with minimum absolute difference of their sums
- Split squares of first N natural numbers into two sets with minimum absolute difference of their sums
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Find the K-th Permutation Sequence of first N natural numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

