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++
#include <bits/stdc++.h>
using namespace std;
void printArrays( int n)
{
vector< int > A, B;
for ( int i = 1; i <= 2 * n; i++) {
if (i % 2 == 0)
A.push_back(i);
else
B.push_back(i);
}
cout << "{ " ;
for ( int i = 0; i < n; i++) {
cout << A[i];
if (i != n - 1)
cout << ", " ;
}
cout << " }\n" ;
cout << "{ " ;
for ( int i = 0; i < n; i++) {
cout << B[i];
if (i != n - 1)
cout << ", " ;
}
cout << " }" ;
}
int main()
{
int N = 5;
printArrays(N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static void printArrays( int n)
{
ArrayList<Integer> A = new ArrayList<Integer>();
ArrayList<Integer> B = new ArrayList<Integer>();
for ( int i = 1 ; i <= 2 * n; i++)
{
if (i % 2 == 0 )
A.add(i);
else
B.add(i);
}
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" );
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( " }" );
}
public static void main (String[] args)
{
int N = 5 ;
printArrays(N);
}
}
|
Python3
def printArrays(n) :
A, B = [], [];
for i in range ( 1 , 2 * n + 1 ):
if (i % 2 = = 0 ) :
A.append(i);
else :
B.append(i);
print ( "{ " , end = "");
for i in range (n) :
print (A[i], end = "");
if (i ! = n - 1 ) :
print ( ", " , end = "");
print ( "}" );
print ( "{ " , end = "");
for i in range (n) :
print (B[i], end = "");
if (i ! = n - 1 ) :
print ( "," , end = " " );
print ( " }" , end = "");
if __name__ = = "__main__" :
N = 5 ;
printArrays(N);
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void printArrays( int n)
{
List< int > A = new List< int >();
List< int > B = new List< int >();
for ( int i = 1; i <= 2 * n; i++)
{
if (i % 2 == 0)
A.Add(i);
else
B.Add(i);
}
Console.Write( "{ " );
for ( int i = 0; i < n; i++)
{
Console.Write(A[i]);
if (i != n - 1)
Console.Write( ", " );
}
Console.Write( " }\n" );
Console.Write( "{ " );
for ( int i = 0; i < n; i++)
{
Console.Write(B[i]);
if (i != n - 1)
Console.Write( ", " );
}
Console.Write( " }" );
}
public static void Main()
{
int N = 5;
printArrays(N);
}
}
|
Javascript
<script>
function printArrays(n)
{
let A = [];
let B = [];
for (let i = 1; i <= 2 * n; i++)
{
if (i % 2 == 0)
A.push(i);
else
B.push(i);
}
document.write( "{ " );
for (let i = 0; i < n; i++)
{
document.write(A[i]);
if (i != n - 1)
document.write( ", " );
}
document.write( " }" + "</br>" );
document.write( "{ " );
for (let i = 0; i < n; i++)
{
document.write(B[i]);
if (i != n - 1)
document.write( ", " );
}
document.write( " }" );
}
let N = 5;
printArrays(N);
</script>
|
Output: { 2, 4, 6, 8, 10 }
{ 1, 3, 5, 7, 9 }
Time Complexity: O(N)
Auxiliary Space: O(N)