
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Minimum swaps required to bring all elements less than or equal to k together in C++
Problem statement
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.
Example
If input array is = {1, 5, 4, 7, 2, 10} and k = 6 then 1 swap is required i.e. swap element 7 with 2.
Algorithm
- Count all elements which are less than or equals to ‘k’. Let’s say the count is ‘cnt’
- Using two pointer technique for window of length ‘cnt’,each time keep track of how many elements in this range are greater than ‘k’. Let’s say the total count is ‘outOfRange’.
- Repeat step 2, for every window of length ‘cnt’ and take minimum of count ‘outOfRange’ among them. This will be the final answer.
Example
#include <bits/stdc++.h> using namespace std; int getMinSwaps(int *arr, int n, int k) { int cnt = 0; for (int i = 0; i < n; ++i) { if (arr[i] <= k) { ++cnt; } } int outOfRange = 0; for (int i = 0; i < cnt; ++i) { if (arr[i] > k) { ++outOfRange; } } int result = outOfRange; for (int i = 0, j = cnt; j < n; ++i, ++j) { if (arr[i] > k) { --outOfRange; } if (arr[j] > k) { ++outOfRange; } result = min(result, outOfRange); } return result; } int main() { int arr[] = {1, 5, 4, 7, 2, 10}; int n = sizeof(arr) / sizeof(arr[0]); int k = 6; cout << "Minimum swaps = " << getMinSwaps(arr, n, k) << endl; return 0; }
When you compile and execute above program. It generates following output −
Output
Minimum swaps = 1
- Related Articles
- Minimum Swaps required to group all 1’s together in C++
- Minimum Swaps to Group All 1's Together in Python
- Minimum operations required to make all the array elements equal in C++
- Program to find minimum swaps needed to group all 1s together in Python
- Program to count number of swaps required to group all 1s together in Python
- Print triplets with sum less than or equal to k in C Program
- Find all factorial numbers less than or equal to n in C++
- Print all prime numbers less than or equal to N in C++
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k in C++
- Print all Semi-Prime Numbers less than or equal to N in C++
- Minimum Swaps to Make Strings Equal in C++
- Count sub-arrays which have elements less than or equal to X in C++
- Program to find minimum number of swaps needed to arrange all pair of socks together in C++
- Linear magnification produced by a concave mirror may be:(a) less than 1 or equal to 1 (b) more than 1 or equal to 1(c) less than 1, more than 1 or equal to 1 (d) less than 1 or more than 1
- Linear magnification produced by a convex lens can be:(a) less than 1 or more than 1.(b) less than 1 or equal to 1.(c) more than 1 or equal to 1.(d) less than 1, equal to 1 or more than 1.

Advertisements