Minimum number of moves required to sort Array by swapping with X
Last Updated :
17 Jul, 2021
Given an integer array, arr[] of size N and an integer X. The task is to sort the array in increasing order in a minimum number of moves by swapping any array element greater than X with X any number of times. If it is not possible print -1.
Examples:
Input: arr[] = {1, 3, 4, 6, 5}, X = 2
Output: 3
Explanation: Swap arr[1] = 3 with X = 2, arr[] becomes {1, 2, 4, 6, 5} and X = 3.
Swap arr[2] = 4 with X = 3, arr[] becomes {1, 2, 3, 6, 5} and X = 4.
Swap arr[3] = 6 with X = 4, arr[] becomes {1, 2, 3, 4, 5}.
Input: arr[] = {7, 5}, X = 6
Output: -1
Explanation: It is not possible to sort the array using the given conditions.
Approach: The given problem can be solved by using the Greedy Approach. Follow the steps below to solve the problem:
- Initialize a variable ans as 0 to store the required result.
- Traverse the array, arr[] in the range [0, N-1] using the variable i
- If the value of arr[i]>arr[i+1], iterate in the range [0, i] using the variable j and swap arr[j] with X, if the value of arr[j]>X, while incrementing the value of ans by 1.
- Check if the array is sorted. If not, then update ans to -1.
- Print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minSwaps( int a[], int n, int x)
{
int c = 0;
int ans = 0;
for ( int i = 0; i < n - 1; i++) {
if (a[i] > a[i + 1]) {
for ( int k = 0; k <= i; k++) {
if (a[k] > x) {
int tt = a[k];
a[k] = x;
x = tt;
ans++;
}
}
}
}
for ( int i = 0; i < n - 1; i++) {
if (a[i] > a[i + 1]) {
c = 1;
break ;
}
}
if (c == 1) {
cout << "-1" ;
}
else {
cout << ans;
}
}
int main()
{
int n = 5;
int x = 2;
int a[] = { 1, 3, 4, 6, 5 };
minSwaps(a, n, x);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void minSwaps( int a[], int n, int x)
{
int c = 0 ;
int ans = 0 ;
for ( int i = 0 ; i < n - 1 ; i++) {
if (a[i] > a[i + 1 ]) {
for ( int k = 0 ; k <= i; k++) {
if (a[k] > x) {
int tt = a[k];
a[k] = x;
x = tt;
ans++;
}
}
}
}
for ( int i = 0 ; i < n - 1 ; i++) {
if (a[i] > a[i + 1 ]) {
c = 1 ;
break ;
}
}
if (c == 1 ) {
System.out.println( "-1" );
}
else {
System.out.println(ans);
}
}
public static void main (String[] args)
{
int n = 5 ;
int x = 2 ;
int a[] = { 1 , 3 , 4 , 6 , 5 };
minSwaps(a, n, x);
}
}
|
Python3
def minSwaps(a, n, x):
c = 0
ans = 0
for i in range (n - 1 ):
if (a[i] > a[i + 1 ]):
for k in range (i + 1 ):
if (a[k] > x):
tt = a[k]
a[k] = x
x = tt
ans + = 1
for i in range (n - 1 ):
if (a[i] > a[i + 1 ]):
c = 1
break
if (c = = 1 ):
print ( "-1" )
else :
print (ans)
if __name__ = = '__main__' :
n = 5
x = 2
a = [ 1 , 3 , 4 , 6 , 5 ]
minSwaps(a, n, x)
|
C#
using System;
class GFG{
static void minSwaps( int [] a, int n, int x)
{
int c = 0;
int ans = 0;
for ( int i = 0; i < n - 1; i++)
{
if (a[i] > a[i + 1])
{
for ( int k = 0; k <= i; k++)
{
if (a[k] > x)
{
int tt = a[k];
a[k] = x;
x = tt;
ans++;
}
}
}
}
for ( int i = 0; i < n - 1; i++)
{
if (a[i] > a[i + 1])
{
c = 1;
break ;
}
}
if (c == 1)
{
Console.Write( "-1" );
}
else
{
Console.Write(ans);
}
}
static public void Main()
{
int n = 5;
int x = 2;
int [] a = { 1, 3, 4, 6, 5 };
minSwaps(a, n, x);
}
}
|
Javascript
<script>
function minSwaps(a, n, x) {
let c = 0;
let ans = 0;
for (let i = 0; i < n - 1; i++) {
if (a[i] > a[i + 1]) {
for (let k = 0; k <= i; k++) {
if (a[k] > x) {
let tt = a[k];
a[k] = x;
x = tt;
ans++;
}
}
}
}
for (let i = 0; i < n - 1; i++) {
if (a[i] > a[i + 1]) {
c = 1;
break ;
}
}
if (c == 1) {
document.write( "-1" );
}
else {
document.write(ans);
}
}
let n = 5;
let x = 2;
let a = [1, 3, 4, 6, 5];
minSwaps(a, n, x);
</script>
|
Time complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...