Minimize swaps required to make the first and last elements the largest and smallest elements in the array respectively
Last Updated :
14 May, 2021
Given an array arr[] consisting of N integers, the task is to find the minimum number of adjacent swaps required to make the first and the last elements in the array the largest and smallest element present in the array respectively.
Examples:
Input: arr[] = {1, 3, 2}
Output: 2
Explanation:
Initially the array is {1, 3, 2}.
Operation 1: Swap element at index 0 and 1. The array modifies to {3, 1, 2}.
Operation 2: Swap element at index 1 and 2. The array modifies to {3, 2, 1}.
Therefore, the minimum number of operation required is 2.
Input: arr[] = {100, 95, 100, 100, 88}
Output: 0
Approach: Follow the steps below to solve the given problem:
- Initialize a variable, say, count, to store the total number of swaps required.
- Find the minimum and the maximum element of the array and store it in a variable, say minElement and maxElement respectively.
- If the minimum and maximum elements are found to be the same, the array consists of a single distinct element. Therefore, print 0 as both the elements are at their correct positions respectively.
- Otherwise, traverse the array and find the index of the first occurrence of maxElement and the last occurrence of minElement and store it in a variable, say minIndex and maxIndex respectively.
- Update the value of count as the sum of maxIndex and (N – 1 – minIndex) as the number of swap operations required to make the largest and the smallest element as the first and the last elements of the array respectively.
- If minIndex is less than maxIndex, then decrease count by 1, as one swap operation will overlap.
- After completing the above steps, print the value of count as the minimum number of swaps required.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimum_swaps( int arr[], int n)
{
int count = 0;
int max_el = *max_element(arr,
arr + n);
int min_el = *min_element(arr,
arr + n);
if (min_el == max_el)
return 0;
int index_max = -1;
int index_min = -1;
for ( int i = 0; i < n; i++) {
if (arr[i] == max_el
&& index_max == -1) {
index_max = i;
}
if (arr[i] == min_el) {
index_min = i;
}
}
count += index_max;
count += (n - 1 - index_min);
if (index_min < index_max)
count -= 1;
return count;
}
int main()
{
int arr[] = { 2, 4, 1, 6, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << minimum_swaps(arr, N);
return 0;
}
|
Java
import java.util.Arrays;
import java.util.Collections;
class GFG{
static int minimum_swaps(Integer arr[], int n)
{
int count = 0 ;
int max_el = Collections.max(Arrays.asList(arr));
int min_el = Collections.min(Arrays.asList(arr));
if (min_el == max_el)
return 0 ;
int index_max = - 1 ;
int index_min = - 1 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] == max_el &&
index_max == - 1 )
{
index_max = i;
}
if (arr[i] == min_el)
{
index_min = i;
}
}
count += index_max;
count += (n - 1 - index_min);
if (index_min < index_max)
count -= 1 ;
return count;
}
public static void main (String[] args)
{
Integer arr[] = { 2 , 4 , 1 , 6 , 5 };
int N = arr.length;
System.out.println(minimum_swaps(arr, N));
}
}
|
Python3
def minimum_swaps(arr, n):
count = 0
max_el = max (arr)
min_el = min (arr)
if (min_el = = max_el):
return 0
index_max = - 1
index_min = - 1
for i in range (n):
if (arr[i] = = max_el and
index_max = = - 1 ):
index_max = i
if (arr[i] = = min_el):
index_min = i
count + = index_max
count + = (n - 1 - index_min)
if (index_min < index_max):
count - = 1
return count
if __name__ = = '__main__' :
arr = [ 2 , 4 , 1 , 6 , 5 ]
N = len (arr)
print (minimum_swaps(arr, N))
|
C#
using System;
using System.Linq;
class GFG{
static int minimum_swaps( int [] arr, int n)
{
int count = 0;
int max_el = arr.Max();
int min_el = arr.Min();
if (min_el == max_el)
return 0;
int index_max = -1;
int index_min = -1;
for ( int i = 0; i < n; i++)
{
if (arr[i] == max_el &&
index_max == -1)
{
index_max = i;
}
if (arr[i] == min_el)
{
index_min = i;
}
}
count += index_max;
count += (n - 1 - index_min);
if (index_min < index_max)
count -= 1;
return count;
}
public static void Main( string [] args)
{
int [] arr = { 2, 4, 1, 6, 5 };
int N = arr.Length;
Console.WriteLine(minimum_swaps(arr, N));
}
}
|
Javascript
<script>
function minimum_swaps(arr, n) {
var count = 0;
var max_el = Math.max(...arr);
var min_el = Math.min(...arr);
if (min_el === max_el) return 0;
var index_max = -1;
var index_min = -1;
for ( var i = 0; i < n; i++) {
if (arr[i] === max_el && index_max === -1) {
index_max = i;
}
if (arr[i] === min_el) {
index_min = i;
}
}
count += index_max;
count += n - 1 - index_min;
if (index_min < index_max) count -= 1;
return count;
}
var arr = [2, 4, 1, 6, 5];
var N = arr.length;
document.write(minimum_swaps(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...