Construct two N-length arrays with same-indexed elements as co-prime and a difference of N in their sum

• Difficulty Level : Expert
• Last Updated : 11 Jun, 2021

Given a positive integer N, the task is to generate two arrays of length N such that the same-indexed elements of both the arrays are co-prime and absolute difference between the sum of elements of the arrays is N.

Examples:

Input: N = 5
Output:
{1, 3, 5, 7, 9}
{2, 4, 6, 8, 10}
Explanation: Pairs of same-indexed elements are (1, 2), (3, 4), (5, 6), (7, 8), (9, 10). It can be observed that all the pairs are coprime and the absolute difference of the sum of the two arrays is 5.

Input: N = 3
Output:
{2, 4, 7}
{1, 3, 6}

Approach: The idea is based on the observation that two consecutive natural numbers are always co-prime and the difference between them is 1. Follow the steps below to solve the problem:

• Initialize two arrays A[] and B[] of size N.
• Iterate over the range [1, 2*N] using the variable i. For every element in the range, check if i is divisible by 2 or not. If found to be true, then insert i into the array A[]. Otherwise, insert i into the array B[].
• After completing the above steps, print the two arrays.

Below is the implementation of the above approach:

C++

 // C++ program for the above approach#include using namespace std; // Function to generate two arrays// satisfying the given conditionsvoid printArrays(int n){    // Declare the two arrays A and B    vector A, B;     // Iterate from range [1, 2*n]    for (int i = 1; i <= 2 * n; i++) {         // Assign consecutive numbers to        // same indices of the two arrays        if (i % 2 == 0)            A.push_back(i);        else            B.push_back(i);    }     // Print the first array    cout << "{ ";    for (int i = 0; i < n; i++) {        cout << A[i];         if (i != n - 1)            cout << ", ";    }    cout << " }\n";     // Print the second array, B    cout << "{ ";    for (int i = 0; i < n; i++) {        cout << B[i];         if (i != n - 1)            cout << ", ";    }    cout << " }";} // Driver Codeint main(){    int N = 5;     // Function Call    printArrays(N);     return 0;}

Java

 // Java program for the above approachimport java.io.*;import java.util.*;  class GFG{       // Satisfying the given conditionsstatic void printArrays(int n){         // Declare the two arrays A and B    ArrayList A = new ArrayList();    ArrayList B = new ArrayList();         // Iterate from range [1, 2*n]    for(int i = 1; i <= 2 * n; i++)    {                 // Assign consecutive numbers to        // same indices of the two arrays        if (i % 2 == 0)            A.add(i);        else            B.add(i);    }      // Print the first array    System.out.print("{ ");    for(int i = 0; i < n; i++)    {        System.out.print(A.get(i));          if (i != n - 1)            System.out.print(", ");    }    System.out.print(" }\n");      // Print the second array, B    System.out.print("{ ");    for(int i = 0; i < n; i++)    {        System.out.print(B.get(i));                 if (i != n - 1)            System.out.print(", ");    }    System.out.print(" }");}  // Driver codepublic static void main (String[] args){    int N = 5;         // Function Call    printArrays(N);}} // This code is contributed by sanjoy_62

Python3

 # Python3 program for the above approach # Function to generate two arrays# satisfying the given conditionsdef printArrays(n) :         # Declare the two arrays A and B    A, B = [], [];     # Iterate from range [1, 2*n]    for i in range(1, 2 * n + 1):                 # Assign consecutive numbers to        # same indices of the two arrays        if (i % 2 == 0) :            A.append(i);        else :            B.append(i);     # Print the first array    print("{ ", end="");    for i in range(n) :        print(A[i], end="");         if (i != n - 1) :            print(", ", end="");         print("}");     # Print the second array, B    print("{ ", end="");         for i in range(n) :        print(B[i], end="");         if (i != n - 1) :            print(",", end=" ");                 print(" }", end=""); # Driver Codeif __name__ == "__main__" :         N = 5;     # Function Call    printArrays(N);     # This code is contributed by AnkitRai01

C#

 // C# program for the above approachusing System;using System.Collections.Generic; class GFG{   // Satisfying the given conditionsstatic void printArrays(int n){         // Declare the two arrays A and B    List A = new List();    List B = new List();          // Iterate from range [1, 2*n]    for(int i = 1; i <= 2 * n; i++)    {                 // Assign consecutive numbers to        // same indices of the two arrays        if (i % 2 == 0)            A.Add(i);        else            B.Add(i);    }       // Print the first array    Console.Write("{ ");    for(int i = 0; i < n; i++)    {        Console.Write(A[i]);           if (i != n - 1)            Console.Write(", ");    }    Console.Write(" }\n");       // Print the second array, B    Console.Write("{ ");    for(int i = 0; i < n; i++)    {        Console.Write(B[i]);                  if (i != n - 1)            Console.Write(", ");    }    Console.Write(" }");}   // Driver Codepublic static void Main(){    int N = 5;         // Function Call    printArrays(N);}} // This code is contributed by susmitakundugoaldanga

Javascript


Output:
{ 2, 4, 6, 8, 10 }
{ 1, 3, 5, 7, 9 }

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

My Personal Notes arrow_drop_up