

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++
|
Time Complexity: O(N)
Auxiliary Space: O(N)