Given an array arr[] of size N, the task is to find the minimum count of adjacent swaps required to rearrange the array elements such that the largest and the smallest array element present on the first and the last indices of the array respectively.
Examples:
Input: arr[] = {33, 44, 11, 12}
Output: 2
Explanation:
Swapping the pair (arr[0], arr[1]) modifies arr[] to {44, 33, 11, 12}
Swapping the pair (arr[2], arr[3]) modifies arr[] to {44, 33, 12, 11}
Therefore, the required output is 2.
Input: arr[]={11, 12, 58, 1, 78, 40, 76}
Output: 6
Approach: Follow the steps below to solve the problem:
- Traverse the array and calculate the index of the first occurrence of the largest array element say, X, and the last occurrence of the smallest array element say, Y.
- The count of adjacent swaps required to move the largest array element at the first index is equal to X.
- The count of adjacent swaps required to move the smallest array element at the last index is equal to N – 1 – Y.
- If X > Y, then one adjacent swap common in moving the largest element at the first index and the smallest element at the last index. Therefore, the total count of adjacent swaps required is equal to X + (N – 1 – Y) – 1.
- Otherwise, the total count of adjacent swaps required is equal to X + (N – 1 – Y).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumMoves( int * a, int n)
{
int min_element = INT_MAX;
int max_element = INT_MIN;
int min_ind = -1;
int max_ind = -1;
for ( int i = 0; i < n; i++) {
if (a[i] <= min_element) {
min_element = a[i];
min_ind = i;
}
if (a[i] > max_element) {
max_element = a[i];
max_ind = i;
}
}
if (max_ind == min_ind) {
return 0;
}
else if (max_ind > min_ind) {
return max_ind + (n - min_ind - 2);
}
else {
return max_ind + n - min_ind - 1;
}
}
int main()
{
int arr[] = { 35, 46, 17, 23 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << minimumMoves(arr, N) << endl;
}
|
Java
import java.util.*;
class GFG
{
static int minimumMoves( int []a, int n)
{
int min_element = Integer.MAX_VALUE;
int max_element = Integer.MIN_VALUE;
int min_ind = - 1 ;
int max_ind = - 1 ;
for ( int i = 0 ; i < n; i++)
{
if (a[i] <= min_element)
{
min_element = a[i];
min_ind = i;
}
if (a[i] > max_element) {
max_element = a[i];
max_ind = i;
}
}
if (max_ind == min_ind) {
return 0 ;
}
else if (max_ind > min_ind) {
return max_ind + (n - min_ind - 2 );
}
else {
return max_ind + n - min_ind - 1 ;
}
}
public static void main(String[] args)
{
int arr[] = { 35 , 46 , 17 , 23 };
int N = arr.length;
System.out.print(minimumMoves(arr, N) + "\n" );
}
}
|
Python3
import sys
def minimumMoves(a, n):
min_element = sys.maxsize
max_element = - sys.maxsize - 1
min_ind = - 1
max_ind = - 1
for i in range (n):
if (a[i] < = min_element):
min_element = a[i]
min_ind = i
if (a[i] > max_element):
max_element = a[i]
max_ind = i
if (max_ind = = min_ind):
return 0
elif (max_ind > min_ind):
return max_ind + (n - min_ind - 2 )
else :
return max_ind + n - min_ind - 1
if __name__ = = '__main__' :
arr = [ 35 , 46 , 17 , 23 ]
N = len (arr)
print (minimumMoves(arr, N))
|
C#
using System;
public class GFG
{
static int minimumMoves( int []a, int n)
{
int min_element = int .MaxValue;
int max_element = int .MinValue;
int min_ind = -1;
int max_ind = -1;
for ( int i = 0; i < n; i++)
{
if (a[i] <= min_element)
{
min_element = a[i];
min_ind = i;
}
if (a[i] > max_element)
{
max_element = a[i];
max_ind = i;
}
}
if (max_ind == min_ind)
{
return 0;
}
else if (max_ind > min_ind)
{
return max_ind + (n - min_ind - 2);
}
else
{
return max_ind + n - min_ind - 1;
}
}
public static void Main(String[] args)
{
int []arr = { 35, 46, 17, 23 };
int N = arr.Length;
Console.Write(minimumMoves(arr, N) + "\n" );
}
}
|
Javascript
<script>
function minimumMoves(a, n)
{
let min_element = Number.MAX_VALUE;
let max_element = Number.MIN_VALUE;
let min_ind = -1;
let max_ind = -1;
for (let i = 0; i < n; i++)
{
if (a[i] <= min_element)
{
min_element = a[i];
min_ind = i;
}
if (a[i] > max_element) {
max_element = a[i];
max_ind = i;
}
}
if (max_ind == min_ind) {
return 0;
}
else if (max_ind > min_ind) {
return max_ind + (n - min_ind - 2);
}
else {
return max_ind + n - min_ind - 1;
}
}
let arr = [ 35, 46, 17, 23 ];
let N = arr.length;
document.write(minimumMoves(arr, N) + "\n" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)