Wednesday, December 7, 2022
HomeSoftware DevelopmentRearrange the given Array to make it sorted and GCD of components...

# Rearrange the given Array to make it sorted and GCD of components until i is Okay

Given a optimistic integer arr[] of size L (2 ≤ L ≤ 2000) containing components 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 doable”.

We now have to know the next two issues:

1. By which case of L and R association is feasible.
2. Association of components.

Let’s talk about them one after the other.

• To Examine association is feasible or not:-

Let’s take an random instance arr[] = {3, 1, 2}, Then all doable 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 provides 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 components (excluding instances during which association is just not doable):
1. 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 full sum of GCD equal to Okay for all legitimate values of L and R.
2. 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 now we have printed it at beginning of association). See examples under 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 happy 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 is just not 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 happy in addition to in sorted format from index 1 to N – 1.

RELATED ARTICLES