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++
#include <bits/stdc++.h>
using namespace std;
bool check( int a[], int n)
{
int b[n];
int minElement = INT_MAX;
for ( int i = 0; i < n; i++)
{
b[i] = a[i];
minElement = min(minElement, a[i]);
}
sort(b, b + n);
int k = 1;
for ( int i = 0; i < n; i++)
{
if (a[i] != b[i] &&
a[i] % minElement != 0)
{
k = 0;
break ;
}
}
return k == 1 ? true : false ;
}
int main()
{
int a[] = { 4, 3, 6, 6, 2, 9 };
int n = sizeof (a) / sizeof (a[0]);
if (check(a, n) == true )
cout << "Yes \n" ;
else
cout<< "No \n" ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
import java.math.*;
class GFG {
public static boolean check( int [] a, int n)
{
int [] b = new int [n];
int minElement = Integer.MAX_VALUE;
for ( int i = 0 ; i < n; i++) {
b[i] = a[i];
minElement
= Math.min(minElement, a[i]);
}
Arrays.sort(b);
int k = 1 ;
for ( int i = 0 ; i < n; i++) {
if (a[i] != b[i]
&& a[i] % minElement != 0 ) {
k = 0 ;
break ;
}
}
return k == 1 ? true : false ;
}
public static void main(String[] args)
{
int [] a = { 4 , 3 , 6 , 6 , 2 , 9 };
int n = a.length;
if (check(a, n) == true ) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
}
|
Python3
import sys
def check(a, n):
b = [ None ] * n
minElement = sys.maxsize
for i in range ( 0 , n):
b[i] = a[i]
minElement = min (minElement, a[i])
b.sort()
k = 1
for i in range ( 0 , n):
if ((a[i] ! = b[i]) and
(a[i] % minElement ! = 0 )):
k = 0
break
if k = = 1 :
return True
else :
return False
if __name__ = = "__main__" :
a = [ 4 , 3 , 6 , 6 , 2 , 9 ]
n = len (a)
if check(a, n) = = True :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool check( int [] a, int n)
{
int [] b = new int [n];
int minElement = int .MaxValue;
for ( int i = 0; i < n; i++)
{
b[i] = a[i];
minElement = Math.Min(minElement, a[i]);
}
Array.Sort(b);
int k = 1;
for ( int i = 0; i < n; i++)
{
if (a[i] != b[i] &&
a[i] % minElement != 0)
{
k = 0;
break ;
}
}
return k == 1 ? true : false ;
}
static public void Main()
{
int [] a = { 4, 3, 6, 6, 2, 9 };
int n = a.Length;
if (check(a, n) == true )
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function check(a, n)
{
let b = new Array(n).fill(0);
let minElement = Number.MAX_VALUE;
for (let i = 0; i < n; i++) {
b[i] = a[i];
minElement
= Math.min(minElement, a[i]);
}
b.sort();
let k = 1;
for (let i = 0; i < n; i++) {
if (a[i] != b[i]
&& a[i] % minElement != 0) {
k = 0;
break ;
}
}
return k == 1 ? true : false ;
}
let a = [ 4, 3, 6, 6, 2, 9 ];
let n = a.length;
if (check(a, n) == true ) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
</script>
|
Time Complexity: O(N logN)
Auxiliary Space: O(N)