Related Articles
Minimum adjacent swaps to move maximum and minimum to corners
• Difficulty Level : Easy
• Last Updated : 20 Apr, 2021

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.

1. Case 1: If l < r: Number of swaps = l + (n-r-1)
2. 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

## 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 ``using` `namespace` `std;` `// Function that returns the minimum swaps``void` `solve(``int` `a[], ``int` `n)``{``    ``int` `maxx = -1, minn = a, 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);``    ``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, 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

 ` ``\$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

 ``
Output:
`2`

Time Complexity: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA

My Personal Notes arrow_drop_up