Construct Array with elements in given range and distinct GCD of each element with its index
Last Updated :
18 Aug, 2022
Given 3 integers N, L and R. The task is to construct an array A[] of N integers, such that :
- Each element of the array is in the range [L, R].
- GCD(i, A[i]) are distinct for all elements.
Examples :
Input : N = 5, L = 1, R = 5
Output : {1, 2, 3, 4, 5}
Explanation : It can be seen that each element is in the range [1, 5].
Also, for i = 1, GCD(1, 1)=1, for i = 2, GCD(2, 2) = 2, for i = 3,
GCD(3, 3) = 3, for i = 4, GCD(4, 4) = 4 and for i = 5, GCD(5, 5) = 5.
Hence, all of these are distinct.
Input : N = 10, L = 30, R = 35
Output : -1
Explanation : It is not possible to construct an array
satisfying the given conditions.
Approach: The approach of the problem is based on the following observation
To satisfy the given conditions, we will have to assure GCD(i, A[i]) = i, for each index of the array from 1 to N.
The idea is to find the smallest possible element with gcd(i, A[i]) = i, larger than or equal to L for each i, and if that element is smaller than equal to R, then we append it, otherwise we return -1(means not possible).
The problem can be solved by the following approach:
- Iterate from i = 1 to N.
- For each i, find the minimum multiple of i that is strictly greater than L − 1.
- Check if that multiple is less than or equal to R.
- If so, that multiple would be the ith element of the array.
- Else, array construction would not be possible.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void constructArray( int N, int L, int R)
{
int A[N + 1];
bool flag = true ;
for ( int i = 1; i <= N; i++) {
A[i] = ((L - 1) / i + 1) * i;
if (A[i] > R) {
flag = false ;
break ;
}
}
if (flag == false ) {
cout << -1;
}
else {
for ( int i = 1; i <= N; i++) {
cout << A[i] << " " ;
}
}
}
int main()
{
int N = 5, L = 1, R = 5;
constructArray(N, L, R);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void constructArray( int N, int L, int R)
{
int A[] = new int [N + 1 ];
boolean flag = true ;
for ( int i = 1 ; i <= N; i++) {
A[i] = ((L - 1 ) / i + 1 ) * i;
if (A[i] > R) {
flag = false ;
break ;
}
}
if (flag == false ) {
System.out.print(- 1 );
}
else {
for ( int i = 1 ; i <= N; i++) {
System.out.print(A[i] + " " );
}
}
}
public static void main(String[] args)
{
int N = 5 , L = 1 , R = 5 ;
constructArray(N, L, R);
}
}
|
Python3
def constructArray(N, L, R) :
A = [ 0 ] * (N + 1 );
flag = True ;
for i in range ( 1 , N + 1 ) :
A[i] = ((L - 1 ) / / i + 1 ) * i;
if (A[i] > R) :
flag = False ;
break ;
if (flag = = False ) :
print ( - 1 );
else :
for i in range ( 1 , N + 1 ) :
print (A[i], end = " " );
if __name__ = = "__main__" :
N = 5 ; L = 1 ; R = 5 ;
constructArray(N, L, R);
|
C#
using System;
class GFG
{
public static void constructArray( int N, int L, int R)
{
int []A = new int [N + 1];
bool flag = true ;
for ( int i = 1; i <= N; i++) {
A[i] = ((L - 1) / i + 1) * i;
if (A[i] > R) {
flag = false ;
break ;
}
}
if (flag == false ) {
Console.Write(-1);
}
else {
for ( int i = 1; i <= N; i++) {
Console.Write(A[i] + " " );
}
}
}
public static void Main( string [] args)
{
int N = 5, L = 1, R = 5;
constructArray(N, L, R);
}
}
|
Javascript
<script>
function constructArray(N, L, R)
{
let A= new Array(N + 1);
let flag = true ;
for (let i = 1; i <= N; i++) {
A[i] = ((L - 1) / i + 1) * i;
if (A[i] > R) {
flag = false ;
break ;
}
}
if (flag == false ) {
document.write(-1);
}
else {
for (let i = 1; i <= N; i++) {
document.write(A[i] + " " );
}
}
}
let N = 5, L = 1, R = 5;
constructArray(N, L, R);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...