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++
// C++ code to implement the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to Construct an array whose elements
// are in given range and GCD of each element
// with its index is distinct
void constructArray(int N, int L, int R)
{
// Declaring the array
int A[N + 1];
bool flag = true;
// Constructing each element of array
for (int i = 1; i <= N; i++) {
// Creating ith element of the array
// as the smallest multiple of i
// which is greater than L-1
A[i] = ((L - 1) / i + 1) * i;
// Checking if the ith created element
// is less than or equal to R
if (A[i] > R) {
flag = false;
break;
}
}
// If flag is false, that implies
// it is not possible to construct the array
if (flag == false) {
cout << -1;
}
// Else print the constructed array
else {
for (int i = 1; i <= N; i++) {
cout << A[i] << " ";
}
}
}
// Driver Code
int main()
{
int N = 5, L = 1, R = 5;
// Function call
constructArray(N, L, R);
return 0;
}
Java
// Java code to implement the above approach
import java.io.*;
class GFG {
// Function to Construct an array whose elements
// are in given range and GCD of each element
// with its index is distinct
public static void constructArray(int N, int L, int R)
{
// Declaring the array
int A[] = new int[N + 1];
boolean flag = true;
// Constructing each element of array
for (int i = 1; i <= N; i++) {
// Creating ith element of the array
// as the smallest multiple of i
// which is greater than L-1
A[i] = ((L - 1) / i + 1) * i;
// Checking if the ith created element
// is less than or equal to R
if (A[i] > R) {
flag = false;
break;
}
}
// If flag is false, that implies
// it is not possible to construct the array
if (flag == false) {
System.out.print(-1);
}
// Else print the constructed array
else {
for (int i = 1; i <= N; i++) {
System.out.print(A[i] + " ");
}
}
}
// Driver Code
public static void main(String[] args)
{
int N = 5, L = 1, R = 5;
// Function call
constructArray(N, L, R);
}
}
// This code is contributed by Rohit Pradhan
Python3
# Python3 code to implement the above approach
# Function to Construct an array whose elements
# are in given range and GCD of each element
# with its index is distinct
def constructArray(N, L, R) :
# Declaring the array
A = [0] * (N + 1);
flag = True;
# Constructing each element of array
for i in range(1, N + 1) :
# Creating ith element of the array
# as the smallest multiple of i
# which is greater than L-1
A[i] = ((L - 1) // i + 1) * i;
# Checking if the ith created element
# is less than or equal to R
if (A[i] > R) :
flag = False;
break;
# If flag is false, that implies
# it is not possible to construct the array
if (flag == False) :
print(-1);
# Else print the constructed array
else :
for i in range(1, N + 1) :
print(A[i], end=" ");
# Driver Code
if __name__ == "__main__" :
N = 5; L = 1; R = 5;
# Function call
constructArray(N, L, R);
# This code is contributed by AnkThon
C#
// C# code to implement the above approach
using System;
class GFG
{
// Function to Construct an array whose elements
// are in given range and GCD of each element
// with its index is distinct
public static void constructArray(int N, int L, int R)
{
// Declaring the array
int []A = new int[N + 1];
bool flag = true;
// Constructing each element of array
for (int i = 1; i <= N; i++) {
// Creating ith element of the array
// as the smallest multiple of i
// which is greater than L-1
A[i] = ((L - 1) / i + 1) * i;
// Checking if the ith created element
// is less than or equal to R
if (A[i] > R) {
flag = false;
break;
}
}
// If flag is false, that implies
// it is not possible to construct the array
if (flag == false) {
Console.Write(-1);
}
// Else print the constructed array
else {
for (int i = 1; i <= N; i++) {
Console.Write(A[i] + " ");
}
}
}
// Driver Code
public static void Main(string[] args)
{
int N = 5, L = 1, R = 5;
// Function call
constructArray(N, L, R);
}
}
// This code is contributed by AnkThon
JavaScript
<script>
// Javascript code to implement the above approach
// Function to Construct an array whose elements
// are in given range and GCD of each element
// with its index is distinct
function constructArray(N, L, R)
{
// Declaring the array
let A= new Array(N + 1);
let flag = true;
// Constructing each element of array
for (let i = 1; i <= N; i++) {
// Creating ith element of the array
// as the smallest multiple of i
// which is greater than L-1
A[i] = ((L - 1) / i + 1) * i;
// Checking if the ith created element
// is less than or equal to R
if (A[i] > R) {
flag = false;
break;
}
}
// If flag is false, that implies
// it is not possible to construct the array
if (flag == false) {
document.write(-1);
}
// Else print the constructed array
else {
for (let i = 1; i <= N; i++) {
document.write(A[i] + " ");
}
}
}
// Driver Code
let N = 5, L = 1, R = 5;
// Function call
constructArray(N, L, R);
// This code is contributed by satwik4409.
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Construct an Array such that GCD of each element with their indices is unique 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 = 15O
7 min read
Find numbers in range [L, R] that are coprime with given Array elements Given an array arr[] consisting of N distinct positive integers and a range [L, R], the task is to find the element in the given range [L, R] that are coprime with all array elements. Examples: Input: L = 3, R = 11, arr[ ] = {4, 7, 9, 6, 13, 21}Output: {5, 11}Explanation:The elements in the range [3
7 min read
Generate Array of distinct elements with GCD as 1 and no Coprime pair Given an integer N, the task is to generate an array of N distinct integers. Such that the GCD of all the elements of the array is 1, but no pair of elements is co-prime i.e., for any pair (A[i], A[j]) of array GCD(A[i], A[j]) â 1. Note: If more than 1 answers are possible, print any of them. Exampl
13 min read
Maximize count of distinct elements possible in an Array from the given operation Given an array A[] of size N, the task is to maximize the count of distinct elements in the array by inserting the absolute differences of the existing array elements. Examples: Input: A[] = {1, 2, 3, 5} Output: 5 Explanation: Possible absolute differences among the array elements are: (2 - 1) = 1 (
6 min read
Counting elements with GCD in range of A, modified to B Given two arrays A and B with lengths N and M respectively, the problem is to count the number of elements in array B that are equal to the greatest common divisor (gcd) of a specific range in array A, where the range is defined by two indices L and R. You are allowed to change one element in the ra
10 min read
Count of integers that divide all the elements of the given array Given an array arr[] of N elements. The task is to find the count of positive integers that divide all the array elements. Examples: Input: arr[] = {2, 8, 10, 6} Output: 2 1 and 2 are the only integers that divide all the elements of the given array. Input: arr[] = {6, 12, 18, 12, 6} Output: 4 Appro
5 min read
Rearrange the given Array to make it sorted and GCD of elements till i is K Given a positive integer arr[] of length L (2 ? L ? 2000) containing elements from 1 to L in unsorted order and an integer K (1 ? K ? 2 * L - 1). Then, the task is to output the arrangement of arr[] by following two conditions: ?GCDi = K for all (0 ? i ? L - 1), where GCDi = GCD(A0, A1,. . . . , Ai)
11 min read
Count of distinct GCDs among all the non-empty subsequences of given array Given an integer array arr[] of size N, the task is to calculate the total number of distinct Greatest Common Divisors(GCDs) among all the non-empty subsequences of arr[]. Examples: Input: arr[]={3,4,8} N=3Output:4Explanation:The different non-empty subsequences possible are {3}, {4}, {8}, {3,4}, {4
8 min read
Modify array by replacing each element with nearest power of GCD of all previous elements Given an array arr[] consisting of N positive integers, the task is to replace each array element with the nearest power of GCD of all the preceding array elements. If there exists more than one possible answer, then print any one of them. Examples: Input: arr[] = {4, 2, 8, 2}Output: 4 1 8 2Explanat
9 min read
Find X such that elements at only alternate indices in given Array are divisible by X Given an array arr[] of N integers, the task is to find an integer X such that the integers that are divisible by X and the integers that are not divisible by X are alternative to each other in the array. If there is no such value print -1. Examples: Input: arr[] = {6, 5, 9, 10, 12, 15}Output : 5Exp
9 min read