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-decreasingbool 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 approachimport 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 approachimport sys# Function to check if the array# can be made non-decreasingdef 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 Codeif __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 approachusing System;class GFG{// Function to check if the array// can be made non-decreasingstatic 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 Codestatic 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 |
Yes
Time Complexity: O(N logN)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Check if array can be sorted by swapping pairs having GCD equal to the smallest element in the array
- Check if array can be sorted by swapping pairs with GCD of set bits count equal to that of the smallest array element
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Smallest subsequence having GCD equal to GCD of given array
- Minimum cost required to rearrange a given array to make it equal to another given array
- Count of all possible pairs having sum of LCM and GCD equal to N
- Minimum deletions required to make GCD of the array equal to 1
- Split array into minimum number of subarrays having GCD of its first and last element exceeding 1
- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- Count of pairs having each element equal to index of the other from an Array
- Split array into two subsequences having minimum count of pairs with sum equal to X
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Count of quadruplets from range [L, R] having GCD equal to K
- Count pairs of natural numbers with GCD equal to given number
- Pairs with GCD equal to one in the given range
- Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array
- Find minimum GCD of all pairs in an array
- Minimum gcd operations to make all array elements one
- Minimum operations to make GCD of array a multiple of k
- Minimum removals from array to make GCD greater
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : akhilsaini

