Construct an Array such that GCD of each element with their indices is unique
Last Updated :
01 Sep, 2022
Given two integers L and R, construct an array of given size N such that the GCD (Greatest Common Divisor) of the element with their indices (1-based indexing) is unique for every index. The task is to print the array or output -1 if it is impossible to do so.
Examples:
Input: N = 10, L = 1, R = 15
Output: 1 2 3 4 5 6 7 8 9 10
Explanation: GCD(Arr[i], i) at every index starting from 1 till N is {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} are all distinct.
Input: N = 5, L = 11, R = 100
Output: 11 12 12 12 15
Approach: To solve the problem use the following idea:
GCD of any number with their indices at max can be the indices itself so we need to find any multiple in the range L to R for the given number to have distinct set of GCD’s for the whole array
Follow the steps to solve the given problem:
- Initialize an array of size N and create a variable pos initially true to store if an answer exists.
- Iterate the array for all indices from 1 to N (1-based indexing).
- Find the first number greater than L which is divisible by the current index.
- if the number is greater than R update pos to false and terminate the loop.
- else store the number in the array and continue the iteration.
- Print the array as the final output.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int L = 11, R = 100;
int N = 5;
bool pos = true ;
int arr[N];
for ( int i = 0; i < N; i++) {
int idx = i + 1;
if (L % idx == 0) {
arr[i] = L;
}
else {
int first_divisible = ((L / idx) + 1) * idx;
if (first_divisible > R) {
pos = false ;
break ;
}
arr[i] = first_divisible;
}
}
if (pos) {
for ( int i = 0; i < N; i++) {
cout << arr[i] << " " ;
}
}
else {
cout << "-1" << endl;
}
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int L = 11 , R = 100 ;
int N = 5 ;
boolean pos = true ;
int arr[] = new int [N];
for ( int i = 0 ; i < N; i++)
{
int idx = i + 1 ;
if (L % idx == 0 )
{
arr[i] = L;
}
else
{
int first_divisible = ((L / idx) + 1 ) * idx;
if (first_divisible > R)
{
pos = false ;
break ;
}
arr[i] = first_divisible;
}
}
if (pos)
{
for ( int i = 0 ; i < N; i++) {
System.out.print(arr[i] + " " );
}
}
else
{
System.out.println(- 1 );
}
}
}
|
Python3
if __name__ = = "__main__" :
L = 11 ; R = 100 ;
N = 5 ;
pos = True ;
arr = [ 0 ] * N;
for i in range (N) :
idx = i + 1 ;
if (L % idx = = 0 ) :
arr[i] = L;
else :
first_divisible = ((L / / idx) + 1 ) * idx;
if (first_divisible > R) :
pos = False ;
break ;
arr[i] = first_divisible;
if (pos) :
for i in range (N) :
print (arr[i], end = " " );
else :
print ( "-1" )
|
C#
using System;
class GFG
{
public static void Main()
{
int L = 11, R = 100;
int N = 5;
bool pos = true ;
int [] arr = new int [N];
for ( int i = 0; i < N; i++)
{
int idx = i + 1;
if (L % idx == 0)
{
arr[i] = L;
}
else
{
int first_divisible = ((L / idx) + 1) * idx;
if (first_divisible > R)
{
pos = false ;
break ;
}
arr[i] = first_divisible;
}
}
if (pos)
{
for ( int i = 0; i < N; i++) {
Console.Write(arr[i] + " " );
}
}
else
{
Console.Write(-1);
}
}
}
|
Javascript
<script>
let L = 11, R = 100;
let N = 5;
let pos = true ;
let arr = new Array(N);
for (let i = 0; i < N; i++)
{
let idx = i + 1;
if (L % idx == 0)
{
arr[i] = L;
}
else
{
let first_divisible = (Math.floor(L / idx) + 1) * idx;
if (first_divisible > R)
{
pos = false ;
break ;
}
arr[i] = first_divisible;
}
}
if (pos)
{
for (let i = 0; i < N; i++) {
document.write(arr[i] + " " );
}
}
else
{
document.write(-1);
}
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N), for creating the array of size N.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...