Sort a given Array by swapping only pairs with GCD as 1
Last Updated :
16 Dec, 2021
Given an array arr[], the task is to check if it is possible to sort the given array using any number of operations where at each operation, two elements arr[i] and arr[j] can be swapped if GCD of arr[i] and arr[j] is 1.
Example:
Input: a = {3, 2, 1}
Output: Possible
Explanation: The given array can be sorted by swapping arr[0] and arr[2], i.e, 3 and 1 as gcd(3, 1) = 1.
Input: arr[] = {10, 15, 6, 2, 9}
Output: Not Possible
Explanation: It is not possible to sort the given array using any sequence of valid operations.
Input: arr[] = {1, 9, 3, 7, 2, 4}
Output: Possible
Approach: The given problem can be solved with the help of recursion and backtracking. Create a recursive function to iterate over all inversions of the given array, swap the inverted elements one at a time if their GCD is 1 and recursively call for the remaining array. At any point, if the array is sorted which can be checked using the is_sorted() function, return true otherwise, return false.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPossible(vector< int > arr)
{
if (is_sorted(arr.begin(), arr.end())) {
return true ;
}
bool res = false ;
for ( int i = 0; i < arr.size(); i++) {
for ( int j = i + 1; j < arr.size(); j++) {
if (arr[i] > arr[j]
&& __gcd(arr[i], arr[j]) == 1) {
swap(arr[i], arr[j]);
res = (res | isPossible(arr));
swap(arr[i], arr[j]);
}
}
}
return res;
}
int main()
{
vector< int > arr = { 1, 9, 3, 7, 2, 4 };
if (isPossible(arr))
cout << "Possible" ;
else
cout << "Not Possible" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean isPossible( int [] arr)
{
if (is_sorted(arr)) {
return true ;
}
boolean res = false ;
for ( int i = 0 ; i < arr.length; i++) {
for ( int j = i + 1 ; j < arr.length; j++) {
if (arr[i] > arr[j]
&& __gcd(arr[i], arr[j]) == 1 ) {
arr = swap(arr,i,j);
res = (res | isPossible(arr));
arr = swap(arr,i,j);
}
}
}
return res;
}
static int __gcd( int a, int b)
{
return b == 0 ? a:__gcd(b, a % b);
}
static int [] swap( int []arr, int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
private static boolean is_sorted( int [] a) {
int i;
for (i = 0 ; i < a.length - 1 && a[i] < a[i+ 1 ]; i++){}
return (i == a.length - 1 );
}
public static void main(String[] args)
{
int [] arr = { 1 , 9 , 3 , 7 , 2 , 4 };
if (isPossible(arr))
System.out.print( "Possible" );
else
System.out.print( "Not Possible" );
}
}
|
Python3
def __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)
def isPossible(arr):
brr = sorted (arr)
if (arr = = brr):
return True
res = False
for i in range ( len (arr)):
for j in range (i + 1 , len (arr)):
if (arr[i] > arr[j] and __gcd(arr[i], arr[j]) = = 1 ):
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
res = (res | isPossible(arr))
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
return res
arr = [ 1 , 9 , 3 , 7 , 2 , 4 ]
if (isPossible(arr)):
print ( "Possible" )
else :
print ( "Not Possible" )
|
C#
using System;
public class GFG{
static bool isPossible( int [] arr)
{
if (is_sorted(arr)) {
return true ;
}
bool res = false ;
for ( int i = 0; i < arr.Length; i++) {
for ( int j = i + 1; j < arr.Length; j++) {
if (arr[i] > arr[j]
&& __gcd(arr[i], arr[j]) == 1) {
arr = swap(arr,i,j);
res = (res | isPossible(arr));
arr = swap(arr,i,j);
}
}
}
return res;
}
static int __gcd( int a, int b)
{
return b == 0? a:__gcd(b, a % b);
}
static int [] swap( int []arr, int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
private static bool is_sorted( int [] a) {
int i;
for (i = 0; i < a.Length - 1 && a[i] < a[i+1]; i++){}
return (i == a.Length - 1);
}
public static void Main(String[] args)
{
int [] arr = { 1, 9, 3, 7, 2, 4 };
if (isPossible(arr))
Console.Write( "Possible" );
else
Console.Write( "Not Possible" );
}
}
|
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 isPossible(arr)
{
brr = arr.sort( function (a, b) { return a - b })
if (arr == brr) {
return true ;
}
let res = false ;
for (let i = 0; i < arr.length; i++)
{
for (let j = i + 1; j < arr.length; j++)
{
if (arr[i] > arr[j]
&& __gcd(arr[i], arr[j]) == 1) {
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
res = (res | isPossible(arr));
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return res;
}
let arr = [1, 9, 3, 7, 2, 4];
if (isPossible(arr))
document.write( "Possible" )
else
document.write( "Not Possible" );
</script>
|
Time Complexity: O(N2N!)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...