Given a constructive integer arr[] of size L (2 ≤ L ≤ 2000) containing parts from 1 to L in unsorted order and an integer Okay
(1 ≤ Okay ≤ 2 * L – 1). Then, the duty is to output the association of arr[] by following two circumstances:
If such an association doesn’t exist then print “Not attainable”.
Now we have to know the next two issues:
- Through which case of L and R association is feasible.
- Association of parts.
Let’s talk about them one after the other.
- To Test association is feasible or not:-
Let’s take an random instance arr[] = {3, 1, 2}, Then all attainable preparations of arr[] are:
First association: arr[] = {1, 2, 3}
GCD0 = GCD(1) = 1
GCD1 = GCD(1, 2)= 1
GCD2 = GCD(1, 2, 3) = 1
Complete sum of GCD = 1 + 1 + 1 = 3
Second association: arr[] = {1, 3, 2}
Complete sum of GCD = 1 + 1 + 1 = 3
Third association: arr[] = {2, 1, 3}
Complete sum of GCD = 2 + 1 + 1 = 4
Fourth association: arr[] = {2, 3, 1}
Complete sum of GCD = 2 + 1 + 1 = 4
Fifth association: arr[] = {3, 1, 2}
Complete sum of GCD = 3 + 1 + 1 = 5
Sixth association: arr[] = {3, 2, 1}
Complete sum of GCD = 3 + 1 + 1 = 5
For all above association we are able to conclude that the Minimal worth and Most worth of complete sum of GCD is the same as 3 and 5 respectively. This offers us concept that association is feasible when R lies within the vary of [L, L+1.., 2*L-1]. In above preparations L = 3 and Max and min worth of Okay is 3(equal to L) and 5(equal to 2 * L – 1) respectively.
- Now Association of parts (excluding instances through which association will not be attainable):
- From above preparations we are able to conclude that when Okay = L, Then, It may be verified that printing counting from 1 to L will give the whole sum of GCD equal to Okay for all legitimate values of L and R.
- For remainder of the instances(When L != R), Take first_element as ((Okay % L) + 1) and print it. Then print remainder of the weather from 1 to L excluding first_element(to keep away from duplicate, as a result of we’ve printed it at beginning of association). See examples beneath for readability:
Instance 1: arr[] = {2, 3, 4, 1, 5}, Okay = 5
L = 5, Okay = 5, we are able to clearly see that L is the same as R, Then print counting from 1 to L:
New association = {1, 2, 3, 4, 5} = GCD0 + GCD1 + GCD2 + GCD3 + GCD4 = 1 + 1 + 1 + 1 + 1 = 5, Complete sum of GCD is the same as Okay, Therefore association glad in addition to in sorted format from index 1 to N – 1.
Instance 2: arr[] = {2, 3, 4, 1, 5}, Okay = 7
L = 5, Okay = 7, we are able to clearly see that L will not be equal to R, Then first_element = ((Okay % L) + 1) = ((7 % 5) + 1) = 2 + 1 = 3.
Now print first_element at first index of association and then counting from 1 to L excluding first_element
New association = {3, 1, 2, 4, 5} = GCD0 + GCD1 + GCD2 + GCD3 + GCD4 = 3 + 1 + 1 + 1 + 1 = 7, Complete sum of GCD is the same as Okay, Therefore association glad in addition to in sorted format from index 1 to N – 1.