Minimize count of swaps of adjacent elements required to make an array increasing
Last Updated :
16 May, 2022
Given an array A[] consisting of permutation of first N natural numbers, the task is to find the minimum number of operations required to modify A[] into an increasing array if an array element A[i] can be swapped with its next adjacent element A[i+1] at most twice. If it is not possible to modify the array into an increasing array, print -1.
Examples:
Input: A[] = [2,1,5,3,4]
Output: 3
Explanation:
Operation 1: Swap (Arr[2], Arr[3]). Therefore, A[] modifies to {2, 1, 3, 5, 4}.
Operation 2: Swap(arr[3], arr[4]). Therefore, A[] modifies to {2, 1, 3, 4, 5}.
Operation 3: Swap (arr[0], arr[1]). Therefore, A[] modifies to {1, 2, 3, 4, 5}.
Therefore, the sequence of operations are: {2, 1, 5, 3, 4} ? {2, 1, 3, 5, 4} ? {2, 1, 3, 4, 5} ? {1, 2, 3, 4, 5}
Input: A[] = [2,5,1,3,4]
Output: -1
Approach: The idea is based on the observation that if an element A[i] is present at index i, then it must have moved from either index i – 1 or i – 2. This is because, to shift to A[i] from indices left of i – 2, the number of swaps would exceed 2. Therefore, traverse the array in reverse, and check if A[i] is present at its correct position or not. If found not to be, check A[i – 1] and A[i – 2] and update the count of operations accordingly. Follow the steps to solve the problem:
- Traverse the array, A[] over the indices [N – 1, 0] using a variable, say i.
- Store the correct index value in a variable, say X as i + 1.
- If the A[i] is currently not at its correct position, i.e. A[i] is not equal to X, then perform the following steps:
- If the value of A[i – 1] is equal to X, increment count by 1 and swap A[i] and A[i-1].
- Otherwise, if the value of A[i – 2] is equal to X, increment count by 2 and swap the pairs A[i – 2] and A[i – 1], then A[i – 2] and A[i].
- Otherwise, update the value of count to -1 and break out of the loop, since A[i] can not be swapped more than twice.
- After complete traversal of the array, print the value of count as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minimumOperations( int A[], int n)
{
int cnt = 0;
for ( int i = n - 1; i >= 0; i--) {
if (A[i] != (i + 1)) {
if (((i - 1) >= 0)
&& A[i - 1] == (i + 1)) {
cnt++;
swap(A[i], A[i - 1]);
}
else if (((i - 2) >= 0)
&& A[i - 2] == (i + 1)) {
cnt += 2;
A[i - 2] = A[i - 1];
A[i - 1] = A[i];
A[i] = i + 1;
}
else {
cout << -1;
return ;
}
}
}
cout << cnt;
}
int main()
{
int A[] = {7, 3, 2, 1, 4 };
int n = sizeof (A) / sizeof (A[0]);
minimumOperations(A, n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG{
static void minimumOperations( int A[], int n)
{
int cnt = 0 ;
for ( int i = n - 1 ; i >= 0 ; i--)
{
if (A[i] != (i + 1 ))
{
if (((i - 1 ) >= 0 ) &&
A[i - 1 ] == (i + 1 ))
{
cnt++;
int t = A[i];
A[i] = A[i- 1 ];
A[i- 1 ] = t;
}
else if (((i - 2 ) >= 0 ) &&
A[i - 2 ] == (i + 1 ))
{
cnt += 2 ;
A[i - 2 ] = A[i - 1 ];
A[i - 1 ] = A[i];
A[i] = i + 1 ;
}
else
{
System.out.println(- 1 );
return ;
}
}
}
System.out.println(cnt);
}
public static void main(String[] args)
{
int A[] = { 7 , 3 , 2 , 1 , 4 };
int n = A.length;
minimumOperations(A, n);
}
}
|
Python3
def minimumOperations(A, n):
cnt = 0
for i in range (n - 1 , - 1 , - 1 ):
if (A[i] ! = (i + 1 )):
if (((i - 1 ) > = 0 )
and A[i - 1 ] = = (i + 1 )):
cnt + = 1
A[i], A[i - 1 ] = A[i - 1 ], A[i]
elif (((i - 2 ) > = 0 )
and A[i - 2 ] = = (i + 1 )):
cnt + = 2
A[i - 2 ] = A[i - 1 ]
A[i - 1 ] = A[i]
A[i] = i + 1
else :
print ( - 1 )
return
print (cnt)
if __name__ = = "__main__" :
A = [ 7 , 3 , 2 , 1 , 4 ]
n = len (A)
minimumOperations(A, n)
|
C#
using System;
class GFG{
static void minimumOperations( int []A, int n)
{
int cnt = 0;
for ( int i = n - 1; i >= 0; i--)
{
if (A[i] != (i + 1))
{
if (((i - 1) >= 0) &&
A[i - 1] == (i + 1))
{
cnt++;
int t = A[i];
A[i] = A[i-1];
A[i-1] = t;
}
else if (((i - 2) >= 0) &&
A[i - 2] == (i + 1))
{
cnt += 2;
A[i - 2] = A[i - 1];
A[i - 1] = A[i];
A[i] = i + 1;
}
else
{
Console.WriteLine(-1);
return ;
}
}
}
Console.WriteLine(cnt);
}
public static void Main()
{
int []A = { 7, 3, 2, 1, 4 };
int n = A.Length;
minimumOperations(A, n);
}
}
|
Javascript
<script>
function minimumOperations(A , n) {
var cnt = 0;
for (i = n - 1; i >= 0; i--) {
if (A[i] != (i + 1)) {
if (((i - 1) >= 0) && A[i - 1] == (i + 1)) {
cnt++;
var t = A[i];
A[i] = A[i - 1];
A[i - 1] = t;
}
else if (((i - 2) >= 0) && A[i - 2] == (i + 1)) {
cnt += 2;
A[i - 2] = A[i - 1];
A[i - 1] = A[i];
A[i] = i + 1;
}
else {
document.write(-1);
return ;
}
}
}
document.write(cnt);
}
var A = [ 7, 3, 2, 1, 4 ];
var n = A.length;
minimumOperations(A, n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...