Sort decreasing permutation of N using triple swaps
Last Updated :
28 Jun, 2022
Given an array A[] consisting of decreasing permutation of N numbers, the task is to sort the array using triple swaps. If it is not possible to sort the array then print -1.
Triple swaps refer to cyclic right shift on chosen indices. Cyclic Right Shift: x –> y –> z –> x.
Examples:
Input: A[] = {4, 3, 2, 1}
Output: 1 2 3 4
Explanation:
For the given array the first step is choosing indexes: x = 0, y = 2, z = 3
Therefore, A[3] = A[2]; A[2] = A[0]; A[0] = A[3].
Before Swapping: 4 3 2 1 and After Swapping: 1 3 4 2.
For the given array the second step is choosing indexes: x = 1, y = 2, z = 3 Therefore, A[3] = A[2]; A[2] = A[1]; A[1] = A[3].
Before Swapping: 1 3 4 2 and After Swapping: 1 2 3 4.
Input: A[] = {5, 4, 3, 2, 1}
Output: 1 2 3 4 5
Explanation:
For the given array the first step is choosing indexes: x = 0, y = 3, z = 4 therefore,
A[4] = A[3]; A[3] = A[0]; A[0] = A[4], Before Swapping: 5 4 3 2 1 and After Swapping: 1 4 3 5 2
For the given array the second step is choosing indexes: x = 1, y = 3, z = 4 therefore,
A[4] = A[3]; A[3] = A[1]; A[1] = A[4], Before Swapping: 1 4 3 5 2 and After Swapping: 1 2 3 4 5
Approach:
To solve the problem mentioned above we have to choose three indexes in such a way so that we can bring at least one element at the correct position. By that, we mean that we have to bring 1 at index 0, 2 at index 1, and so on.
- x is chosen as the current index number i,
- z is chosen as the index of x + 1 which is always N – i – 1 and
- y is chosen accordingly.
Then we have to perform the swapping of elements by the cyclic right shift of elements using these indices.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void sortArray( int A[], int N)
{
int x, y, z;
if (N % 4 == 0 || N % 4 == 1) {
for ( int i = 0; i < N / 2; i++) {
x = i;
if (i % 2 == 0) {
y = N - i - 2;
z = N - i - 1;
}
A[z] = A[y];
A[y] = A[x];
A[x] = x + 1;
}
cout << "Sorted Array: " ;
for ( int i = 0; i < N; i++)
cout << A[i] << " " ;
}
else
cout << "-1" ;
}
int main()
{
int A[] = { 5, 4, 3, 2, 1 };
int N = sizeof (A) / sizeof (A[0]);
sortArray(A, N);
return 0;
}
|
Java
class GFG{
static void sortArray( int A[], int N)
{
int x = 0 , y = 0 , z = 0 ;
if (N % 4 == 0 || N % 4 == 1 )
{
for ( int i = 0 ; i < N / 2 ; i++)
{
x = i;
if (i % 2 == 0 )
{
y = N - i - 2 ;
z = N - i - 1 ;
}
A[z] = A[y];
A[y] = A[x];
A[x] = x + 1 ;
}
System.out.print( "Sorted Array: " );
for ( int i = 0 ; i < N; i++)
System.out.print(A[i] + " " );
}
else
{
System.out.print( "-1" );
}
}
public static void main(String[] args)
{
int A[] = { 5 , 4 , 3 , 2 , 1 };
int N = A.length;
sortArray(A, N);
}
}
|
Python3
def sortArray(A, N):
if (N % 4 = = 0 or N % 4 = = 1 ):
for i in range (N / / 2 ):
x = i
if (i % 2 = = 0 ):
y = N - i - 2
z = N - i - 1
A[z] = A[y]
A[y] = A[x]
A[x] = x + 1
print ( "Sorted Array: " , end = "")
for i in range (N):
print (A[i], end = " " )
else :
print ( "-1" )
A = [ 5 , 4 , 3 , 2 , 1 ]
N = len (A)
sortArray(A, N)
|
C#
using System;
class GFG{
static void sortArray( int []A, int N)
{
int x = 0, y = 0, z = 0;
if (N % 4 == 0 || N % 4 == 1)
{
for ( int i = 0; i < N / 2; i++)
{
x = i;
if (i % 2 == 0)
{
y = N - i - 2;
z = N - i - 1;
}
A[z] = A[y];
A[y] = A[x];
A[x] = x + 1;
}
Console.Write( "Sorted Array: " );
for ( int i = 0; i < N; i++)
Console.Write(A[i] + " " );
}
else
{
Console.Write( "-1" );
}
}
public static void Main(String[] args)
{
int []A = { 5, 4, 3, 2, 1 };
int N = A.Length;
sortArray(A, N);
}
}
|
Javascript
<script>
function sortArray(A, N)
{
let x = 0, y = 0, z = 0;
if (N % 4 == 0 || N % 4 == 1)
{
for (let i = 0; i < N / 2; i++)
{
x = i;
if (i % 2 == 0)
{
y = N - i - 2;
z = N - i - 1;
}
A[z] = A[y];
A[y] = A[x];
A[x] = x + 1;
}
document.write( "Sorted Array: " );
for (let i = 0; i < N; i++)
document.write(A[i] + " " );
}
else
{
document.write( "-1" );
}
}
let A = [ 5, 4, 3, 2, 1 ];
let N = A.length;
sortArray(A, N);
</script>
|
Output:
Sorted Array: 1 2 3 4 5
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...