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 = 4
Output: 4 1 3 2
Explanation:
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 = 7
Output: 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++ program to implement the above approach #include <bits/stdc++.h> using namespace std;
// Function to construct an array with adjacent elements as // co-prime numbers void ConstArrayAdjacentCoprime( int N)
{ // Iterate over the range [1, N]
for ( int i = 1; i <= N; i++)
cout << i << " " ;
} // Driver Code int main()
{ int N = 6;
ConstArrayAdjacentCoprime(N);
return 0;
} // This code is contributed by Sania Kumari Gupta |
// C program to implement the above approach #include <stdio.h> // Function to construct an array with adjacent elements as // co-prime numbers void ConstArrayAdjacentCoprime( int N)
{ // Iterate over the range [1, N]
for ( int i = 1; i <= N; i++)
printf ( "%d " , i);
} // Driver Code int main()
{ int N = 6;
ConstArrayAdjacentCoprime(N);
return 0;
} // This code is contributed by Sania Kumari Gupta |
// Java program to implement // the above approach public class GFG
{ // Function to construct an array 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 |
# Python3 program to implement # the above approach # Function to construct an array 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 |
// C# program to implement // the above approach using System;
class GFG{
// Function to construct an array 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 |
<script> // javascript program to implement // the above approach // Function to construct an array with
// adjacent elements as co-prime numbers
function ConstArrayAdjacentCoprime(N)
{
// Iterate over the range [1, N]
for (let i = 1; i <= N; i++)
{
// Print i
document.write(i + " " );
}
}
// Driver code let N = 6;
ConstArrayAdjacentCoprime(N);
// This code is contributed by target_2.
</script> |
1 2 3 4 5 6
Time Complexity: O(N)
Auxiliary Space: O(1)