Given an array A[], consisting of N elements, the task is to find the minimum number of swaps required such that array elements swapped to replace a higher element, in the original array, is maximized.

Examples:

Input:_ A[] = {4, 3, 3, 2, 5} _

Output:_ 3 _

Explanation:

Swap 1: {4, 3, 3, 2, 5} -> {5_, 3, 3, 2, 4} _

Swap 2: {5, 3, 3, 2, 4} -> {3_, 3, 5, 2, 4} _

_Swap 3: {3, 3, 52, 4} -> {3, 3, 25, 4} _

Therefore, elements {4, 3, 2} occupies original position of a higher element after swapping

Input:_. A[] = {6, 5, 4, 3, 2, 1} _

OutputL_ 5 _

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

**Naive Approach: **The simplest approach to solve the problem can be implemented as follows:

  • **Sort **the array in ascending order.
  • Initialize two variables, _result _and index, to store the count and the index up to which it has been considered in the original array, repectively.
  • Iterate over the array elements. For any element A[i], go to a value in the array which is greater than ai and increment _index _variable accordingly.
  • After finding an element greater than A[i], increment result and index.
  • If _index _has reached the end of the array, no elements are left to be swapped with previously checked elements.
  • Therefore, print count.

Below is the implementation of the above approach:

  • C++
  • Python3

// C++ Program to implement

// the above approach

#include <bits/stdc++.h>

**using** **namespace** std;

// Function to find the minimum

// number of swaps required

**int** countSwaps(``**int** A[], **int** n)

{

// Sort the array in ascending order

sort(A, A + n);

**int** ind = 1, res = 0;

**for** (``**int** i = 0; i < n; i++) {

// Iterate until a greater element

// is found

**while** (ind < n and A[ind] == A[i])

// Keep incrementing ind

ind++;

// If a greater element is found

**if** (ind < n and A[ind] > A[i]) {

// Increase count of swap

res++;

// Increment ind

ind++;

}

// If end of array is reached

**if** (ind >= n)

**break**``;

}

// Return the answer

**return** res;

}

// Driver Code

**int** main()

{

**int** A[] = { 4, 3, 3, 2, 5 };

cout << countSwaps(A, 5);

**return** 0;

}

Output:

3

Time Complexity:_ O(N * log N) _

Auxiliary Space:_ O(1)_

Efficient Approach:

Since any swap between two unequal elements leads to an element replacing a higher element, it can be observed that the minimum number of swaps required is N – (the maximum frequency of an array element). Therefore, find the most frequent element in the array using HashMap, and print the result.

Below is the implementation of the above approach:

  • C++

// C++ Program to implement

// the above approach

#include <bits/stdc++.h>

**using** **namespace** std;

// Function to find the minimum

// number of swaps required

**int** countSwaps(``**int** A[], **int** n)

{

// Stores the frequency of the

// array elements

map<``**int**``, **int**``> mp;

// Stores maximum frequency

**int** max_frequency = 0;

// Find the max frequency

**for** (``**int** i = 0; i < n; i++) {

// Update frequency

mp[A[i]]++;

// Update maximum frequency

max_frequency

= max(max_frequency, mp[A[i]]);

}

**return** n - max_frequency;

}

// Driver Code

**int** main()

{

**int** A[] = { 6, 5, 4, 3, 2, 1 };

// function call

cout << countSwaps(A, 6);

**return** 0;

}

Output:

5

Time Complexity:_ O(N) _

Auxiliary Space:_ 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.

#arrays #greedy #hash #mathematical #array-rearrange #cpp-map #frequency-counting

Minimize swaps required to maximize the count of elements
2.00 GEEK