Minimum swaps required to bring all elements less than or equal to k together

Last Updated : 17 Dec, 2024

Given an array of n positive integers and a number k. Find the minimum number of swaps required to bring all the numbers less than or equal to k together. 

Examples: 

Input: arr[] = {2, 1, 5, 6, 3}, k = 3
Output: 1
Explanation: To bring elements 2, 1, 3 together, swap arr[2] with arr[4]. Final array will be arr[] = {2, 1, 3, 6, 5}

Input: arr[] = {2, 7, 9, 8, 5, 7, 4}, k = 5
Output: 1
Explanation: To bring elements 2, 5 and 4 together, swap arr[0] with arr[5]. Final array will be arr[] = {7, 7, 9, 8, 5, 2, 4}.

Try It Yourself
redirect icon

[Naive Approach] Finding all subarrays - O(N * K) Time and O(1) Space

A simple solution is to first count all elements less than or equal to k(say 'good'). Now traverse for every subarray of size good and swap those elements whose value > k with those whose value <= k and are not included in the subarray. The time complexity of this approach is O(N * K).

[Expected Approach] Using Fixed-Size Sliding Window - O(N) Time and O(1) Space

The idea is to find the number of elements which are <= k, (say 'good'). This means that we need a subarray of size 'good' with all the elements <= k. So, for every window of size 'good', swap elements in the subarray which are greater than k with elements not included in the subarray and are <= k, to find the minimum swaps. For this, we can maintain a sliding window of size 'good' and for each of the sliding window calculate the minimum swaps by finding the count of elements > k, say ('bad') in the window. Minimum swaps among all the sliding windows will be the answer.

Below is the implementation of the above approach:

C++
// C++ program to find Minimum swaps required to bring all
// elements less than or equal to k together

#include <bits/stdc++.h>
using namespace std;

// Function for finding the minimum number of swaps
// required to bring all the numbers less
// than or equal to k together. 
int minSwap(int arr[], int n, int k)
{
    // Find the number of elements <= k
    int good = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] <= k)
            good += 1;
    }

    // Initialize min swaps with good as the max possible
    // value
    int bad = 0, minSwaps = good;
    for (int i = 0; i < n; i++) {
        // If the current element > k, then increment the
        // count of bad elements in the current sliding
        // window
        if (arr[i] > k) {
            bad += 1;
        }
        // If we complete the first sliding window,
        // calculate min swaps
        if (i == good - 1) {
            minSwaps = min(minSwaps, bad);
        }
        else if (i >= good) {
            // Exclude the elements from the start of the
            // sliding window to maintain its size as 'good'
            if (arr[i - good] > k)
                bad -= 1;
            // For every sliding window of size 'good', find
            // the minimum swaps required
            minSwaps = min(minSwaps, bad);
        }
    }
    // Return the minimum swaps
    return minSwaps;
}

// Driver's code
int main()
{
    // Sample Input
    int arr1[] = { 2, 7, 9, 8, 5, 7, 4 };
    int n = sizeof(arr1) / sizeof(arr1[0]);
    int k = 5;

    cout << minSwap(arr1, n, k) << "\n";
    return 0;
}
Java
// Java program to find Minimum swaps required to bring all
// elements less than or equal to k together
import java.util.Arrays;

public class MinSwaps {

    // Function for finding the minimum number of swaps
    // required to bring all the numbers less
    // than or equal to k together.
    static int minSwap(int[] arr, int n, int k) {
        // Find the number of elements <= k
        int good = 0;
        for (int i = 0; i < n; i++) {
            if (arr[i] <= k)
                good += 1;
        }

        // Initialize min swaps with good as the max possible
        // value
        int bad = 0, minSwaps = good;
        for (int i = 0; i < n; i++) {
            // If the current element > k, then increment the
            // count of bad elements in the current sliding
            // window
            if (arr[i] > k) {
                bad += 1;
            }
            // If we complete the first sliding window,
            // calculate min swaps
            if (i == good - 1) {
                minSwaps = Math.min(minSwaps, bad);
            } else if (i >= good) {
                // Exclude the elements from the start of the
                // sliding window to maintain its size as 'good'
                if (arr[i - good] > k)
                    bad -= 1;
                // For every sliding window of size 'good', find
                // the minimum swaps required
                minSwaps = Math.min(minSwaps, bad);
            }
        }
        // Return the minimum swaps
        return minSwaps;
    }

    // Driver's code
    public static void main(String[] args) {
        // Sample Input
        int[] arr1 = { 2, 7, 9, 8, 5, 7, 4 };
        int n = arr1.length;
        int k = 5;

        System.out.println(minSwap(arr1, n, k));
    }
}
Python
# Python program to find Minimum swaps required to bring all
# elements less than or equal to k together

# Function for finding the minimum number of swaps
# required to bring all the numbers less
# than or equal to k together.
def min_swap(arr, n, k):
    # Find the number of elements <= k
    good = 0
    for i in range(n):
        if arr[i] <= k:
            good += 1

    # Initialize min swaps with good as the max possible
    # value
    bad = 0
    min_swaps = good
    for i in range(n):
        # If the current element > k, then increment the
        # count of bad elements in the current sliding
        # window
        if arr[i] > k:
            bad += 1
        
        # If we complete the first sliding window,
        # calculate min swaps
        if i == good - 1:
            min_swaps = min(min_swaps, bad)
        elif i >= good:
            # Exclude the elements from the start of the
            # sliding window to maintain its size as 'good'
            if arr[i - good] > k:
                bad -= 1
            
            # For every sliding window of size 'good', find
            # the minimum swaps required
            min_swaps = min(min_swaps, bad)
    
    # Return the minimum swaps
    return min_swaps

# Sample Input
arr1 = [2, 7, 9, 8, 5, 7, 4]
n = len(arr1)
k = 5

print(min_swap(arr1, n, k))
C#
// C# program to find Minimum swaps required to bring all
// elements less than or equal to k together

using System;

public class MinimumSwaps {

    // Function for finding the minimum number of swaps
    // required to bring all the numbers less
    // than or equal to k together.
    static int MinSwap(int[] arr, int n, int k) {
        // Find the number of elements <= k
        int good = 0;
        for (int i = 0; i < n; i++) {
            if (arr[i] <= k)
                good += 1;
        }

        // Initialize min swaps with good as the max possible
        // value
        int bad = 0, minSwaps = good;
        for (int i = 0; i < n; i++) {
            // If the current element > k, then increment the
            // count of bad elements in the current sliding
            // window
            if (arr[i] > k) {
                bad += 1;
            }
            // If we complete the first sliding window,
            // calculate min swaps
            if (i == good - 1) {
                minSwaps = Math.Min(minSwaps, bad);
            }
            else if (i >= good) {
                // Exclude the elements from the start of the
                // sliding window to maintain its size as 'good'
                if (arr[i - good] > k)
                    bad -= 1;
                // For every sliding window of size 'good', find
                // the minimum swaps required
                minSwaps = Math.Min(minSwaps, bad);
            }
        }
        // Return the minimum swaps
        return minSwaps;
    }

    // Driver's code
    public static void Main() {
        // Sample Input
        int[] arr1 = { 2, 7, 9, 8, 5, 7, 4 };
        int n = arr1.Length;
        int k = 5;

        Console.WriteLine(MinSwap(arr1, n, k));
    }
}
JavaScript
// JavaScript program to find Minimum swaps required to
// bring all elements less than or equal to k together

// Function for finding the minimum number of swaps
// required to bring all the numbers less
// than or equal to k together.
function minSwap(arr, n, k)
{
    // Find the number of elements <= k
    let good = 0;
    for (let i = 0; i < n; i++) {
        if (arr[i] <= k)
            good += 1;
    }

    // Initialize min swaps with good as the max possible
    // value
    let bad = 0, minSwaps = good;
    for (let i = 0; i < n; i++) {
        // If the current element > k, then increment the
        // count of bad elements in the current sliding
        // window
        if (arr[i] > k) {
            bad += 1;
        }
        // If we complete the first sliding window,
        // calculate min swaps
        if (i === good - 1) {
            minSwaps = Math.min(minSwaps, bad);
        }
        else if (i >= good) {
            // Exclude the elements from the start of the
            // sliding window to maintain its size as 'good'
            if (arr[i - good] > k)
                bad -= 1;
            // For every sliding window of size 'good', find
            // the minimum swaps required
            minSwaps = Math.min(minSwaps, bad);
        }
    }
    // Return the minimum swaps
    return minSwaps;
}

// Driver's code
function main()
{
    // Sample Input
    let arr1 = [ 2, 7, 9, 8, 5, 7, 4 ];
    let n = arr1.length;
    let k = 5;

    console.log(minSwap(arr1, n, k));
}

main();

Output
1

Working of the above approach:


Complexity Analysis:

  • Time Complexity: O(n), where n is the size of input array arr[]
  • Auxiliary Space: O(1)
Comment