Rearrange array to make it non-decreasing by swapping pairs having GCD equal to minimum array element
Given an array, arr[] consisting of N positive integers, the task is to make the array non-decreasing by swapping pairs (arr[i], arr[j]) such that i != j (1 ≤ i, j ≤ n) and GCD(arr[i], arr[j]) is equal to the minimum element present in the array.
Examples:
Input: arr[] = {4, 3, 6, 6, 2, 9}
Output: Yes
Explanation:
Minimum array element = 2.
Swap arr[0] and arr[2], since gcd(4, 6) = 2. Array modifies to {6, 3, 4, 6, 2, 9}.
Swap arr[0] and arr[4], since gcd(6, 2) = 2.
Therefore, the modified array {2, 3, 4, 6, 6, 9} is non-decreasing.Input: arr[] = {7, 5, 2, 2, 4}
Output: No
Approach:
- Firstly, traverse the array to find the minimum element. Store all these elements in another array and sort that array.
- For every array element, check if it is at the correct position or not. If found to be true, proceed to next element. Otherwise, check if it is divisible by the smallest element of the array as only these elements will have GCD with other elements equal to the minimum element of the array.
- If any array element is not at its correct position and that element is not divisible by the minimum element of the array, print “No”.
Below is the implementation of the above approach:
C++
// C++ program for above approach #include <bits/stdc++.h> using namespace std; // Function to check if the array // can be made non-decreasing bool check( int a[], int n) { int b[n]; int minElement = INT_MAX; // Iterate till N for ( int i = 0; i < n; i++) { // Find the minimum element b[i] = a[i]; minElement = min(minElement, a[i]); } // Sort the array sort(b, b + n); int k = 1; // Iterate till N for ( int i = 0; i < n; i++) { // Check if the element is // at its correct position if (a[i] != b[i] && a[i] % minElement != 0) { k = 0; break ; } } // Return the answer return k == 1 ? true : false ; } // Driver Code int main() { int a[] = { 4, 3, 6, 6, 2, 9 }; int n = sizeof (a) / sizeof (a[0]); // Print the answer if (check(a, n) == true ) cout << "Yes \n" ; else cout<< "No \n" ; return 0; } // This code is contributed by akhilsaini |
Java
// Java program for above approach import java.io.*; import java.util.*; import java.math.*; class GFG { // Function to check if the array // can be made non-decreasing public static boolean check( int [] a, int n) { int [] b = new int [n]; int minElement = Integer.MAX_VALUE; // Iterate till N for ( int i = 0 ; i < n; i++) { // Find the minimum element b[i] = a[i]; minElement = Math.min(minElement, a[i]); } // Sort the array Arrays.sort(b); int k = 1 ; // Iterate till N for ( int i = 0 ; i < n; i++) { // Check if the element is // at its correct position if (a[i] != b[i] && a[i] % minElement != 0 ) { k = 0 ; break ; } } // Return the answer return k == 1 ? true : false ; } // Driver Code public static void main(String[] args) { int [] a = { 4 , 3 , 6 , 6 , 2 , 9 }; int n = a.length; // Print the answer if (check(a, n) == true ) { System.out.println( "Yes" ); } else { System.out.println( "No" ); } } } |
Python3
# Python3 program for above approach import sys # Function to check if the array # can be made non-decreasing def check(a, n): b = [ None ] * n minElement = sys.maxsize # Iterate till N for i in range ( 0 , n): # Find the minimum element b[i] = a[i] minElement = min (minElement, a[i]) # Sort the array b.sort() k = 1 # Iterate till N for i in range ( 0 , n): # Check if the element is # at its correct position if ((a[i] ! = b[i]) and (a[i] % minElement ! = 0 )): k = 0 break # Return the answer if k = = 1 : return True else : return False # Driver Code if __name__ = = "__main__" : a = [ 4 , 3 , 6 , 6 , 2 , 9 ] n = len (a) # Print the answer if check(a, n) = = True : print ( "Yes" ) else : print ( "No" ) # This code is contributed by akhilsaini |
C#
// C# program for above approach using System; class GFG{ // Function to check if the array // can be made non-decreasing static bool check( int [] a, int n) { int [] b = new int [n]; int minElement = int .MaxValue; // Iterate till N for ( int i = 0; i < n; i++) { // Find the minimum element b[i] = a[i]; minElement = Math.Min(minElement, a[i]); } // Sort the array Array.Sort(b); int k = 1; // Iterate till N for ( int i = 0; i < n; i++) { // Check if the element is // at its correct position if (a[i] != b[i] && a[i] % minElement != 0) { k = 0; break ; } } // Return the answer return k == 1 ? true : false ; } // Driver Code static public void Main() { int [] a = { 4, 3, 6, 6, 2, 9 }; int n = a.Length; // Print the answer if (check(a, n) == true ) Console.WriteLine( "Yes" ); else Console.WriteLine( "No" ); } } // This code is contributed by akhilsaini |
Javascript
<script> // Javascript program to implement // the above approach // Function to check if the array // can be made non-decreasing function check(a, n) { let b = new Array(n).fill(0); let minElement = Number.MAX_VALUE; // Iterate till N for (let i = 0; i < n; i++) { // Find the minimum element b[i] = a[i]; minElement = Math.min(minElement, a[i]); } // Sort the array b.sort(); let k = 1; // Iterate till N for (let i = 0; i < n; i++) { // Check if the element is // at its correct position if (a[i] != b[i] && a[i] % minElement != 0) { k = 0; break ; } } // Return the answer return k == 1 ? true : false ; } // Driver Code let a = [ 4, 3, 6, 6, 2, 9 ]; let n = a.length; // Print the answer if (check(a, n) == true ) { document.write( "Yes" ); } else { document.write( "No" ); } // This code is contributed by avijitmondal1998. </script> |
Output:
Yes
Time Complexity: O(N logN)
Auxiliary Space: O(N)
Please Login to comment...