Given a positive integer **N**, the task is to construct an array consisting of first **N** natural numbers such that each pair of adjacent elements in the array is a co-prime. If multiple solutions exist, then print any one of them.

**Examples:**

Input:N = 4Output:4 1 3 2Explanation:

All possible adjacent pairs of the array are { (4, 1), (1, 3), (3, 2) }

Since all adjacent pairs of the array are co-prime, the required output is 4 1 3 2.

Input:N = 7Output:3 4 5 7 6 1 2

**Approach:** The idea is to iterate over the range **[1, N]** using variable **i** and print the value of **i**. Following are the observations:

GCD(i, i + 1) = 1

Therefore, for all possible value of i the pair (i, i + 1) are coprime numbers.

Below is the implementation of the above approach:

## C++

`// C++ program to implement` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to construct an arrary with` `// adjacent elements as co-prime numbers` `void` `ConstArrayAdjacentCoprime(` `int` `N)` `{` ` ` `// Iterate over the range [1, N]` ` ` `for` `(` `int` `i = 1; i <= N; i++) {` ` ` `// Print i` ` ` `cout << i << ` `" "` `;` ` ` `}` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 6;` ` ` `ConstArrayAdjacentCoprime(N);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement` `// the above approach` `class` `GFG` `{` ` ` `// Function to construct an arrary with` ` ` `// adjacent elements as co-prime numbers` ` ` `static` `void` `ConstArrayAdjacentCoprime(` `int` `N)` ` ` `{` ` ` `// Iterate over the range [1, N]` ` ` `for` `(` `int` `i = ` `1` `; i <= N; i++)` ` ` `{` ` ` ` ` `// Print i` ` ` `System.out.print(i + ` `" "` `);` ` ` `}` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main (String[] args)` ` ` `{` ` ` `int` `N = ` `6` `;` ` ` `ConstArrayAdjacentCoprime(N);` ` ` `}` `}` `// This code is contributed by AnkitRai01` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement` `# the above approach` `# Function to construct an arrary with` `# adjacent elements as co-prime numbers` `def` `ConstArrayAdjacentCoprime(N):` ` ` `# Iterate over the range [1, N]` ` ` `for` `i ` `in` `range` `(` `1` `, N ` `+` `1` `):` ` ` ` ` `# Print i` ` ` `print` `(i, end ` `=` `" "` `)` `# Driver Code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` `N ` `=` `6` ` ` `ConstArrayAdjacentCoprime(N)` `# This code is contributed by AnkitRai01` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement` `// the above approach ` `using` `System;` `class` `GFG{` ` ` `// Function to construct an arrary with` `// adjacent elements as co-prime numbers` `static` `void` `ConstArrayAdjacentCoprime(` `int` `N)` `{` ` ` ` ` `// Iterate over the range [1, N]` ` ` `for` `(` `int` `i = 1; i <= N; i++)` ` ` `{` ` ` ` ` `// Print i` ` ` `Console.Write(i + ` `" "` `);` ` ` `}` `}` `// Driver Code` `public` `static` `void` `Main ()` `{` ` ` `int` `N = 6;` ` ` ` ` `ConstArrayAdjacentCoprime(N);` `}` `}` `// This code is contributed by susmitakundugoaldanga` |

*chevron_right*

*filter_none*

**Output:**

1 2 3 4 5 6

**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.