Given N number of elements, find the minimum number of swaps required so that the maximum element is at the beginning and the minimum element is at last with the condition that only swapping of adjacent elements is allowed.
Examples:
Input: a[] = {3, 1, 5, 3, 5, 5, 2}
Output: 6
Step 1: Swap 5 with 1 to make the array as {3, 5, 1, 3, 5, 5, 2}
Step 2: Swap 5 with 3 to make the array as {5, 3, 1, 3, 5, 5, 2}
Step 3: Swap 1 with 3 at its right to make the array as {5, 3, 3, 1, 5, 5, 2}
Step 4: Swap 1 with 5 at its right to make the array as {5, 3, 3, 5, 1, 5, 2}
Step 5: Swap 1 with 5 at its right to make the array as {5, 3, 3, 5, 5, 1, 2}
Step 6: Swap 1 with 2 at its right to make the array as {5, 3, 3, 5, 5, 2, 1}
After performing 6 swapping operations 5 is at the beginning and 1 at the end
Input: a[] = {5, 6, 1, 3}
Output: 2
The approach will be to find the index of the largest element(let l). Find the index of the leftmost largest element if largest element appears in the array more than once. Similarly, find the index of the rightmost smallest element(let r). There exists two cases to solve this problem.
- Case 1: If l < r: Number of swaps = l + (n-r-1)
- Case 2: If l > r: Number of swaps = l + (n-r-2), as one swap has already been performed while swapping the larger element to the front
Below is the implementation of above approach
C++
#include <bits/stdc++.h>
using namespace std;
void solve( int a[], int n)
{
int maxx = -1, minn = a[0], l = 0, r = 0;
for ( int i = 0; i < n; i++) {
if (a[i] > maxx) {
maxx = a[i];
l = i;
}
if (a[i] <= minn) {
minn = a[i];
r = i;
}
}
if (r < l)
cout << l + (n - r - 2);
else
cout << l + (n - r - 1);
}
int main()
{
int a[] = { 5, 6, 1, 3 };
int n = sizeof (a)/ sizeof (a[0]);
solve(a, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void minimumSwaps( int a[], int n)
{
int maxx = - 1 , minn = a[ 0 ], l = 0 , r = 0 ;
for ( int i = 0 ; i < n; i++) {
if (a[i] > maxx) {
maxx = a[i];
l = i;
}
if (a[i] <= minn) {
minn = a[i];
r = i;
}
}
if (r < l)
System.out.println(l + (n - r - 2 ));
else
System.out.println(l + (n - r - 1 ));
}
public static void main(String args[]) throws IOException
{
int a[] = { 5 , 6 , 1 , 3 };
int n = a.length;
minimumSwaps(a, n);
}
}
|
Python3
def minSwaps(arr):
n = len (arr)
maxx, minn, l, r = - 1 , arr[ 0 ], 0 , 0
for i in range (n):
if arr[i] > maxx:
maxx = arr[i]
l = i
if arr[i] < = minn:
minn = arr[i]
r = i
if r < l:
print (l + (n - r - 2 ))
else :
print (l + (n - r - 1 ))
arr = [ 5 , 6 , 1 , 3 ]
minSwaps(arr)
|
C#
using System;
class GFG
{
public static void minimumSwaps( int []a,
int n)
{
int maxx = -1, l = 0,
minn = a[0], r = 0;
for ( int i = 0; i < n; i++)
{
if (a[i] > maxx)
{
maxx = a[i];
l = i;
}
if (a[i] <= minn)
{
minn = a[i];
r = i;
}
}
if (r < l)
Console.WriteLine(l + (n - r - 2));
else
Console.WriteLine(l + (n - r - 1));
}
public static void Main()
{
int []a = { 5, 6, 1, 3 };
int n = a.Length;
minimumSwaps(a, n);
}
}
|
PHP
<?php
function solve( $a , $n )
{
$maxx = -1; $minn = $a [0];
$l = 0; $r = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a [ $i ] > $maxx )
{
$maxx = $a [ $i ];
$l = $i ;
}
if ( $a [ $i ] <= $minn )
{
$minn = $a [ $i ];
$r = $i ;
}
}
if ( $r < $l )
echo $l + ( $n - $r - 2);
else
echo $l + ( $n - $r - 1);
}
$a = array (5, 6, 1, 3);
$n = count ( $a );
solve( $a , $n );
?>
|
Javascript
<script>
function solve(a, n)
{
let maxx = -1, minn = a[0], l = 0, r = 0;
for (let i = 0; i < n; i++) {
if (a[i] > maxx) {
maxx = a[i];
l = i;
}
if (a[i] <= minn) {
minn = a[i];
r = i;
}
}
if (r < l)
document.write(l + (n - r - 2));
else
document.write( l + (n - r - 1));
}
let a = [ 5, 6, 1, 3 ];
let n = a.length;
solve(a, n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1), since we are passing the array by reference.