# Modify sequence of first N natural numbers to a given array by replacing pairs with their GCD

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:YesExplanation: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 **i**^{th} 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
**i**^{th}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;` `}` |

## Java

`// Java program for the above approach` `class` `GFG{` ` ` `// Function to check if array arr[]` `// can be obtained from first N` `// natural numbers or not` `static` `void` `isSequenceValid(` `int` `[] B,` ` ` `int` `N)` `{` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++)` ` ` `{` ` ` `if` `((i + ` `1` `) % B[i] != ` `0` `)` ` ` `{` ` ` `System.out.print(` `"No"` `);` ` ` `return` `;` ` ` `}` ` ` `}` ` ` `System.out.print(` `"Yes"` `);` `}` ` ` `// Driver code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `N = ` `4` `;` ` ` `int` `[] arr = { ` `1` `, ` `2` `, ` `3` `, ` `2` `};` ` ` ` ` `// Function Call` ` ` `isSequenceValid(arr, N);` `}` `}` ` ` `// This code is contributed by sanjoy_62` |

## Python3

`# Python3 program for the above approach` `# Function to check if array arr[]` `# can be obtained from first N` `# natural numbers or not` `def` `isSequenceValid(B, N):` ` ` ` ` `for` `i ` `in` `range` `(N):` ` ` `if` `((i ` `+` `1` `) ` `%` `B[i] !` `=` `0` `):` ` ` `print` `(` `"No"` `)` ` ` `return` ` ` ` ` `print` `(` `"Yes"` `)` ` ` `# Driver Code` `N ` `=` `4` `arr ` `=` `[ ` `1` `, ` `2` `, ` `3` `, ` `2` `]` ` ` `# Function Call` `isSequenceValid(arr, N)` `# This code is contributed by susmitakundugoaldanga` |

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` ` ` `// Function to check if array arr[]` `// can be obtained from first N` `// natural numbers or not` `static` `void` `isSequenceValid(` `int` `[] B,` ` ` `int` `N)` `{` ` ` `for` `(` `int` `i = 0; i < N; i++)` ` ` `{` ` ` `if` `((i + 1) % B[i] != 0)` ` ` `{` ` ` `Console.WriteLine(` `"No"` `);` ` ` `return` `;` ` ` `}` ` ` `}` ` ` `Console.WriteLine(` `"Yes"` `);` `}` ` ` `// Driver code` `public` `static` `void` `Main()` `{` ` ` `int` `N = 4;` ` ` `int` `[] arr = { 1, 2, 3, 2 };` ` ` ` ` `// Function Call` ` ` `isSequenceValid(arr, N);` `}` `}` `// This code is contributed by code_hunt` |

## Javascript

`<script>` `// Javascript program to implement` `// the above approach` `// Function to check if array arr[]` `// can be obtained from first N` `// natural numbers or not` `function` `isSequenceValid(B, N)` `{` ` ` `for` `(let i = 0; i < N; i++)` ` ` `{` ` ` `if` `((i + 1) % B[i] != 0)` ` ` `{` ` ` `document.write(` `"No"` `);` ` ` `return` `;` ` ` `}` ` ` `}` ` ` `document.write(` `"Yes"` `);` `}` `// Driver code` ` ` `let N = 4;` ` ` `let arr = [ 1, 2, 3, 2 ];` ` ` ` ` `// Function Call` ` ` `isSequenceValid(arr, N);` ` ` ` ` `// This code is contributed by souravghosh0416.` `</script>` |

**Output:**

Yes

**Time Complexity:** O(N)**Auxiliary Space:** O(1)