Check if array can be sorted by swapping pairs with GCD of set bits count equal to that of the smallest array element
Given an array arr[] consisting of N integers, the task is to check if it is possible to sort the array using the following swap operations:
Swapping of two numbers is valid only if the Greatest Common Divisor of count of set bits of the two numbers is equal to the number of set bits in the smallest element of the array.
If it is possible to sort the array by performing only the above swaps, then print “Yes”. Otherwise, print “No”.
Examples:
Input: arr[] = {2, 3, 5, 7, 6}
Output: Yes
Explanation:
7 and 6 are needed to be swapped to make the array sorted.
7 has 3 set bits and 6 has 2 set bits.
Since GCD(2, 3) = 1, which is equal to the number of set bits in the smallest integer from the array i.e., 2 (1 set bit).
Therefore, the array can be sorted by swapping (7, 6).
Input: arr[] = {3, 3, 15, 7}
Output: No
Explanation:
15 and 7 are needed to be swapped to make the array sorted.
15 has 4 set bits and 7 has 3 set bits. GCD(4, 3) = 1, which is not equal to the number of set bits in the smallest integer from the array i.e., 3(2 set bit).
Therefore, the array cannot be sorted.
Approach: Follow the steps below to solve the problem:
- Sort the given array and store it in an auxiliary array(say dup[]).
- Iterate over the array and for every element, check if it is at the same index in both arr[] and dup[] or not. If found to be true, proceed to the next index.
- Otherwise, if swapping of ith and jth position elements is required in arr[] then calculate the GCD of set bits of arr[i] with set bits of arr[j] and check if it is equal to the count of set bits in the smallest element of the array or not.
- If any such swapping is not allowed, print “No”. Otherwise, print “Yes”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int calculateSetBit( int n)
{
int count = 0;
for ( int i = 0; i < 32; i++) {
if (n & 1)
count++;
n = n >> 1;
}
return count;
}
void sortPossible( int arr[], int n)
{
int dup[n];
for ( int i = 0; i < n; i++)
dup[i] = arr[i];
sort(dup, dup + n);
bool flag = 1;
int bit = calculateSetBit(dup[0]);
for ( int i = 0; i < n; i++) {
if (arr[i] != dup[i]) {
if (__gcd(
calculateSetBit(arr[i]),
bit)
!= bit) {
flag = 0;
break ;
}
}
}
if (flag) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return ;
}
int main()
{
int arr[] = { 3, 9, 12, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
sortPossible(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static int gcd( int a, int b)
{
if (a == 0 )
return b;
if (b == 0 )
return a;
if (a == b)
return a;
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
static int calculateSetBit( int n)
{
int count = 0 ;
for ( int i = 0 ; i < 32 ; i++)
{
if ((n & 1 ) != 0 )
count++;
n = n >> 1 ;
}
return count;
}
static void sortPossible( int arr[], int n)
{
int dup[] = new int [n];
for ( int i = 0 ; i < n; i++)
dup[i] = arr[i];
Arrays.sort(dup);
int flag = 1 ;
int bit = calculateSetBit(dup[ 0 ]);
for ( int i = 0 ; i < n; i++)
{
if (arr[i] != dup[i])
{
if (gcd(calculateSetBit(
arr[i]), bit) != bit)
{
flag = 0 ;
break ;
}
}
}
if (flag != 0 )
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
return ;
}
public static void main(String[] args)
{
int arr[] = { 3 , 9 , 12 , 6 };
int N = arr.length;
sortPossible(arr, N);
}
}
|
Python3
from math import gcd
def calculateSetBit(n):
count = 0
for i in range ( 32 ):
if (n & 1 ):
count + = 1
n = n >> 1
return count
def sortPossible(arr, n):
dup = [ 0 ] * n
for i in range (n):
dup[i] = arr[i]
dup = sorted (dup)
flag = 1
bit = calculateSetBit(dup[ 0 ])
for i in range (n):
if (arr[i] ! = dup[i]):
if (gcd(calculateSetBit(arr[i]), bit) ! = bit):
flag = 0
break
if (flag):
print ( "Yes" )
else :
print ( "No" )
return
if __name__ = = '__main__' :
arr = [ 3 , 9 , 12 , 6 ]
N = len (arr)
sortPossible(arr, N)
|
C#
using System;
class GFG{
static int gcd( int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
static int calculateSetBit( int n)
{
int count = 0;
for ( int i = 0; i < 32; i++)
{
if ((n & 1) != 0)
count++;
n = n >> 1;
}
return count;
}
static void sortPossible( int [] arr, int n)
{
int [] dup = new int [n];
for ( int i = 0; i < n; i++)
dup[i] = arr[i];
Array.Sort(dup);
int flag = 1;
int bit = calculateSetBit(dup[0]);
for ( int i = 0; i < n; i++)
{
if (arr[i] != dup[i])
{
if (gcd(calculateSetBit(
arr[i]), bit) != bit)
{
flag = 0;
break ;
}
}
}
if (flag != 0)
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
return ;
}
public static void Main()
{
int [] arr = { 3, 9, 12, 6 };
int N = arr.Length;
sortPossible(arr, N);
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
function calculateSetBit(n)
{
let count = 0;
for (let i = 0; i < 32; i++)
{
if ((n & 1) != 0)
count++;
n = n >> 1;
}
return count;
}
function sortPossible(arr, n)
{
let dup = [];
for (let i = 0; i < n; i++)
dup[i] = arr[i];
dup.sort();
let flag = 1;
let bit = calculateSetBit(dup[0]);
for (let i = 0; i < n; i++)
{
if (arr[i] != dup[i])
{
if (gcd(calculateSetBit(
arr[i]), bit) != bit)
{
flag = 0;
break ;
}
}
}
if (flag != 0)
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
return ;
}
let arr = [ 3, 9, 12, 6 ];
let N = arr.length;
sortPossible(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
01 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...