Minimum adjacent swaps to move maximum and minimum to corners | Java program to count Minimum number of swaps so that the largest element is at beginning and the smallest element is at last ; Function performing calculations ; Index of leftmost largest element ; Index of rightmost smallest element ; Driver Code
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 ) ; } }