Open In App

Rearrange the given Array to make it sorted and GCD of elements till i is K

Last Updated : 09 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a positive integer arr[] of length L (2 ? L ? 2000) containing elements from 1 to L in unsorted order and an integer
(1 ? K ? 2 * L – 1). Then, the task is to output the arrangement of arr[] by following two conditions:  

  •  ?GCDi = K for all (0 ? i ? L – 1), where GCDi = GCD(A0, A1,.  . . . , Ai)
  •  arr[] should be in sorted format from A1 to AN – 1

 If such an arrangement doesn’t exist then print “Not possible”.

Examples:

Input: arr[] = {3, 2, 4, 1}, X = 5
Output: 2 1 3 4  

Input: arr[] = {3, 2, 4, 1, 6, 5},  X = 2
Output: Not Possible

Approach: Implement the below idea to solve the problem

Arrangement of such arr[] is only possible when (L ? K ? 2 * L – 1). For output of arrangement see the illustration  of approach.

Illustration of approach:

We have to know the following two things:

  1. In which case of L and R arrangement is possible.
  2. Arrangement of elements.

Let’s discuss them one by one.

  • To Check arrangement is possible or not:-

 Let’s take an random example arr[] = {3, 1, 2}, Then all possible arrangements of arr[] are:

First arrangement: arr[] = {1, 2, 3} 

GCD0 = GCD(1) = 1
GCD1 = GCD(1, 2)= 1
GCD2 = GCD(1, 2, 3) = 1  

Total sum of GCD = 1 + 1 + 1 = 3 

Second arrangement: arr[] = {1, 3, 2}  

Total sum of GCD = 1 + 1 + 1 = 3 

Third arrangement: arr[] = {2, 1, 3}  

Total sum of GCD = 2 + 1 + 1 = 4

 Fourth arrangement: arr[] = {2, 3, 1}  

Total sum of GCD = 2 + 1 + 1 = 4

Fifth arrangement: arr[] = {3, 1, 2}  

Total sum of GCD = 3 + 1 + 1 = 5

Sixth arrangement: arr[] = {3, 2, 1}  

Total sum of GCD = 3 + 1 + 1 = 5 

For all above arrangement we can conclude that the Minimum value and Maximum value of total sum of GCD is equal to 3 and 5 respectively. This gives us idea that arrangement is possible when R lies in the range of [L, L+1.., 2*L-1]. In above arrangements L = 3 and Max and min value of K is 3(equal to L) and 5(equal to 2 * L – 1) respectively.

  • Now Arrangement of elements (excluding cases in which arrangement is not possible):
  1. From above arrangements we can conclude that when K = L, Then, It can be verified that printing counting from 1 to L will give the total sum of GCD equal to K for all valid values of L and R.
  2. For rest of the cases(When L != R), Take first_element as ((K % L) + 1) and print it. Then print rest of the elements from 1 to L excluding first_element(to avoid duplicate, because we have printed it at starting of arrangement). See examples below for clarity:

Example 1: arr[] = {2, 3, 4, 1, 5}, K = 5

L = 5, K = 5, we can clearly see that L is equal to R,  Then print counting from 1 to L:

New arrangement = {1, 2, 3, 4, 5} = GCD0 + GCD1 + GCD2 + GCD3 + GCD4 = 1 + 1 + 1 + 1 + 1 = 5, Total sum of GCD is equal to K, Hence arrangement satisfied as well as in sorted format from index 1 to N – 1.

Example 2: arr[] = {2, 3, 4, 1, 5}, K = 7

L = 5, K = 7, we can clearly see that L is not equal to R, Then first_element = ((K % L) + 1) = ((7 % 5) + 1) = 2 + 1 = 3.

Now print first_element at first index of arrangement and then counting from 1 to L excluding first_element

New arrangement = {3, 1, 2, 4, 5} = GCD0 + GCD1 + GCD2 + GCD3 + GCD4 = 3 + 1 + 1 + 1 + 1 = 7, Total sum of GCD is equal to K, Hence arrangement satisfied as well as in sorted format from index 1 to N – 1.   

Below is the implementation for the above approach:

C++




// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
 
// Driver Function
int main()
{
 
  // Input arr[]
  vector<int> arr = {6, 4, 1, 2, 3, 5};
 
  // Length of arr[]
  int L = arr.size();
 
  // Input value of K
  int K = 7;
 
  // Condition to check whether
  // arrangement is possible or not.
  if (K <= ((2 * (L)) - 1) && K >= L)
  {
 
    // Counter variable to print
    // rest of the elements
    // excluding first element of
    // arrangement
    int counter = 1;
 
    // First element of arrangement
    int first = (K % L) == 0 ? counter : ((K % L) + 1);
 
    // Printing first element
    // of arrangement
    cout << first << " ";
 
    // Loop for printing rest
    // of the elements
    for (int i = 1; i <= L; i++)
    {
 
      // Condition when counter
      // variable equal to first
      // element of arrangement
      if (first == counter)
      {
 
        // Just increment the
        // counter to skip
        // current value, because
        // current value of counter
        // is equal to first element
        // of arrangement,
        // Which we have printed.
        counter++;
      }
 
      // If counter is not equal
      // to first letter then
      // else will execute
      else
      {
        cout << counter++ << " ";
      }
    }
  }
 
  // If K is not in the range
  // L <= K <= 2 * L - 1,Then
  // printing "Not possible"
  // as output.
  else
  {
    cout << "Not possible" << endl;
  }
}
 
// This code is contributed by Potta Lokesh


Java




// Java code to implement the approach
 
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
    // Driver Function
    public static void main(String[] args)
        throws java.lang.Exception
    {
 
        // Input arr[]
        int[] arr = { 6, 4, 1, 2, 3, 5 };
 
        // Length of arr[]
        int L = arr.length;
 
        // Input value of K
        int K = 7;
 
        // Condition to check whether
        // arrangement is possible or not.
        if (K <= ((2 * (L)) - 1) && K >= L) {
 
            // Counter variable to print
            // rest of the elements
            // excluding first element of
            // arrangement
            int counter = 1;
 
            // First element of arrangement
            int first
                = (K % L) == 0 ? counter : ((K % L) + 1);
 
            // Printing first element
            // of arrangement
            System.out.print(first + " ");
 
            // Loop for printing rest
            // of the elements
            for (int i = 1; i <= L; i++) {
 
                // Condition when counter
                // variable equal to first
                // element of arrangement
                if (first == counter) {
 
                    // Just increment the
                    // counter to skip
                    // current value, because
                    // current value of counter
                    // is equal to first element
                    // of arrangement,
                    // Which we have printed.
                    counter++;
                }
 
                // If counter is not equal
                // to first letter then
                // else will execute
                else {
                    System.out.print((counter++) + " ");
                }
            }
        }
 
        // If K is not in the range
        // L <= K <= 2 * L - 1,Then
        // printing "Not possible"
        // as output.
        else {
            System.out.println("Not possible");
        }
    }
}


Python3




# python code to implement the approach
 
# Driver Function
  # Input arr[]
arr = [6, 4, 1, 2, 3, 5]
 
  # Length of arr[]
L = len(arr)
 
  # Input value of K
K = 7;
 
  # Condition to check whether
  # arrangement is possible or not.
if (K <= ((2 * (L)) - 1) and K >= L):
   
  # Counter variable to print
    # rest of the elements
    # excluding first element of
    # arrangement
  counter = 1
 
    # First element of arrangement
  if(K % L == 0):
    first = counter
  else:
    first = (K % L) + 1
     
  # Printing first element
    # of arrangement
  print(first, end = " ")
   
    # Loop for printing rest
    # of the elements
  for i in range(1,L+1):
     
    # Condition when counter
      # variable equal to first
      # element of arrangement
    if (first == counter):
       
      # Just increment the
        # counter to skip
        # current value, because
        # current value of counter
        # is equal to first element
        # of arrangement,
        # Which we have printed.
      counter += 1
       
      # If counter is not equal
      # to first letter then
      # else will execute
    else:
      print(counter, end = " ")
      counter += 1
       
  # If K is not in the range
  # L <= K <= 2 * L - 1,Then
  # printing "Not possible"
  # as output.
else:
  print("Not possible")
   
# This code is contributed by ksam24000


C#




// C# code to implement the approach
using System;
 
public class GFG {
 
    static public void Main()
    {
        // Input arr[]
        int[] arr = { 6, 4, 1, 2, 3, 5 };
 
        // Length of arr[]
        int L = arr.Length;
 
        // Input value of K
        int K = 7;
 
        // Condition to check whether
        // arrangement is possible or not.
        if (K <= ((2 * (L)) - 1) && K >= L) {
 
            // Counter variable to print
            // rest of the elements
            // excluding first element of
            // arrangement
            int counter = 1;
 
            // First element of arrangement
            int first
                = (K % L) == 0 ? counter : ((K % L) + 1);
 
            // Printing first element
            // of arrangement
            Console.Write(first + " ");
 
            // Loop for printing rest
            // of the elements
            for (int i = 1; i <= L; i++) {
 
                // Condition when counter
                // variable equal to first
                // element of arrangement
                if (first == counter) {
 
                    // Just increment the
                    // counter to skip
                    // current value, because
                    // current value of counter
                    // is equal to first element
                    // of arrangement,
                    // Which we have printed.
                    counter++;
                }
 
                // If counter is not equal
                // to first letter then
                // else will execute
                else {
                    Console.Write((counter++) + " ");
                }
            }
        }
 
        // If K is not in the range
        // L <= K <= 2 * L - 1,Then
        // printing "Not possible"
        // as output.
        else {
            Console.WriteLine("Not possible");
        }
    }
}
 
// This code is contributed by Rohit Pradhan


Javascript




<script>
 
// Javascript code to implement the approach
// Driver Function
 
// Input arr[]
let arr = [6, 4, 1, 2, 3, 5];
 
// Length of arr[]
let L = arr.length;
 
// Input value of K
let K = 7;
 
//output
let ans = [];
 
// Condition to check whether
// arrangement is possible or not.
if (K <= ((2 * (L)) - 1) && K >= L) {
 
    // Counter variable to print
    // rest of the elements
    // excluding first element of
    // arrangement
    let counter = 1;
 
    // First element of arrangement
    let first = (K % L) == 0 ? counter : ((K % L) + 1);
 
    // Printing first element
    // of arrangement
    ans.push(first);
 
    // Loop for printing rest
    // of the elements
    for (let i = 1; i <= L; i++) {
 
        // Condition when counter
        // variable equal to first
        // element of arrangement
        if (first == counter) {
 
            // Just increment the
            // counter to skip
            // current value, because
            // current value of counter
            // is equal to first element
            // of arrangement,
            // Which we have printed.
            counter++;
        }
 
        // If counter is not equal
        // to first letter then
        // else will execute
        else {
            ans.push(counter++);
        }
    }
}
 
// If K is not in the range
// L <= K <= 2 * L - 1,Then
// printing "Not possible"
// as output.
else {
    console.log("Not possible");
}
 
// printing ans;
console.log(ans);
 
// This code is contributed by akashish__
 
</script>


Output

2 1 3 4 5 6 

Time Complexity: O(N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads