Minimum adjacent swaps to move maximum and minimum to corners
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++
// CPP program to count Minimum number // of adjacent /swaps so that the largest element // is at beginning and the smallest element // is at last #include <bits/stdc++.h> using namespace std; // Function that returns the minimum swaps void solve( int a[], int n) { int maxx = -1, minn = a[0], l = 0, r = 0; for ( int i = 0; i < n; i++) { // Index of leftmost largest element if (a[i] > maxx) { maxx = a[i]; l = i; } // Index of rightmost smallest element if (a[i] <= minn) { minn = a[i]; r = i; } } if (r < l) cout << l + (n - r - 2); else cout << l + (n - r - 1); } // Driver Code int main() { int a[] = { 5, 6, 1, 3 }; int n = sizeof (a)/ sizeof (a[0]); solve(a, n); return 0; } |
Java
// Java program to count Minimum number // of swaps so that the largest element // is at beginning and the // smallest element is at last import java.io.*; class GFG { // Function performing calculations 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++) { // Index of leftmost largest element if (a[i] > maxx) { maxx = a[i]; l = i; } // Index of rightmost smallest element 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 )); } // Driver Code public static void main(String args[]) throws IOException { int a[] = { 5 , 6 , 1 , 3 }; int n = a.length; minimumSwaps(a, n); } } |
Python3
# Python3 program to count # Minimum number of adjacent # swaps so that the largest # element is at beginning and # the smallest element is at last. def minSwaps(arr): '''Function that returns the minimum swaps''' n = len (arr) maxx, minn, l, r = - 1 , arr[ 0 ], 0 , 0 for i in range (n): # Index of leftmost # largest element if arr[i] > maxx: maxx = arr[i] l = i # Index of rightmost # smallest element if arr[i] < = minn: minn = arr[i] r = i if r < l: print (l + (n - r - 2 )) else : print (l + (n - r - 1 )) # Driver code arr = [ 5 , 6 , 1 , 3 ] minSwaps(arr) # This code is contributed # by Tuhin Patra |
C#
// C# program to count Minimum // number of swaps so that the // largest element is at beginning // and the smallest element is at last using System; class GFG { // Function performing calculations 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++) { // Index of leftmost // largest element if (a[i] > maxx) { maxx = a[i]; l = i; } // Index of rightmost // smallest element 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)); } // Driver Code public static void Main() { int []a = { 5, 6, 1, 3 }; int n = a.Length; // Calling function minimumSwaps(a, n); } } // This code is contributed by anuj_67. |
PHP
<?php // PHP program to count Minimum // number of adjacent /swaps so // that the largest element is // at beginning and the smallest // element is at last // Function that returns // the minimum swaps function solve( $a , $n ) { $maxx = -1; $minn = $a [0]; $l = 0; $r = 0; for ( $i = 0; $i < $n ; $i ++) { // Index of leftmost // largest element if ( $a [ $i ] > $maxx ) { $maxx = $a [ $i ]; $l = $i ; } // Index of rightmost // smallest element if ( $a [ $i ] <= $minn ) { $minn = $a [ $i ]; $r = $i ; } } if ( $r < $l ) echo $l + ( $n - $r - 2); else echo $l + ( $n - $r - 1); } // Driver Code $a = array (5, 6, 1, 3); $n = count ( $a ); solve( $a , $n ); // This code is contributed // by anuj_67. ?> |
Javascript
<script> // JavaScript program to count Minimum number // of adjacent /swaps so that the largest element // is at beginning and the smallest element // is at last // Function that returns the minimum swaps function solve(a, n) { let maxx = -1, minn = a[0], l = 0, r = 0; for (let i = 0; i < n; i++) { // Index of leftmost largest element if (a[i] > maxx) { maxx = a[i]; l = i; } // Index of rightmost smallest element 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)); } // Driver Code let a = [ 5, 6, 1, 3 ]; let n = a.length; solve(a, n); </script> |
2
Time Complexity: O(N)
Auxiliary Space: O(1), since we are passing the array by reference.
Please Login to comment...