Saturday, November 26, 2022
HomeSoftware DevelopmentRearrange the given Array to make it sorted and GCD of parts...

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


Given a optimistic 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 situations:  

 If such an association doesn’t exist then print “Not attainable”.

We now have to know the next two issues:

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

Let’s focus on them one after the other.

  • To Examine 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 will conclude that the Minimal worth and Most worth of whole 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 circumstances by which association is just not attainable):
  1. From above preparations we will 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.
  2. For remainder of the circumstances(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 beneath for readability:

Instance 1: arr[] = {2, 3, 4, 1, 5}, Okay = 5

L = 5, Okay = 5, we will 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 will 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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments