Given 3 integers N, L and R. The duty is to assemble an array A[] of N integers, such that :
- Every aspect of the array is within the vary [L, R].
- GCD(i, A[i]) are distinct for all parts.
Examples :
Enter : N = 5, L = 1, R = 5
Output : {1, 2, 3, 4, 5}
Clarification : It may be seen that every aspect is within the vary [1, 5].
Additionally, 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.
Therefore, all of those are distinct.Enter : N = 10, L = 30, R = 35
Output : -1
Clarification : It’s not doable to assemble an array
satisfying the given circumstances.
Strategy: The strategy of the issue is predicated on the next commentary
To fulfill the given circumstances, we should guarantee GCD(i, A[i]) = i, for every index of the array from 1 to N.
The concept is to seek out the smallest doable aspect with gcd(i, A[i]) = i, bigger than or equal to L for every i, and if that aspect is smaller than equal to R, then we append it, in any other case we return -1(means not doable).
The issue may be solved by the next strategy:
- Iterate from i = 1 to N.
- For every i, discover the minimal a number of of i that’s strictly larger than L − 1.
- Examine if that a number of is lower than or equal to R.
- If that’s the case, that a number of can be the ith aspect of the array.
- Else, array development wouldn’t be doable.
Beneath is the implementation of the above strategy:
C++
|
Time Complexity: O(N)
Auxiliary Area: O(N)