
- 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 group all 1’s together in C++
Problem statement
Given an array of 0’s and 1’s. The task is to find the minimum number of swaps required to group all 1’s present in the array together.
Example
If input array = {1, 0, 1, 1, 0, 1} then 1 swap is required. i.e. swap first 0 with last 1.
Algorithm
- Count total number of 1’s in the array
- If count is x, then we need to find the subarray of length x of this array with maximum number of 1’s
- Minimum swaps required will be the number of 0’s in the subarray of length x with maximum number of 1’s
Example
#include <bits/stdc++.h> using namespace std; int getMinSwaps(int *arr, int n) { int oneCnt = 0; for (int i = 0; i < n; ++i) { if (arr[i] == 1) { ++oneCnt; } } int x = oneCnt; int maxOnes = INT_MIN; int preCompute[n] = {0}; if (arr[0] == 1) { preCompute[0] = 1; } for (int i = 1; i < n; ++i) { if (arr[i] == 1) { preCompute[i] = preCompute[i - 1] + 1; } else { preCompute[i] = preCompute[i - 1]; } } for (int i = x - 1; i < n; ++i) { if (i == (x - 1)) { oneCnt = preCompute[i]; } else { oneCnt = preCompute[i] - preCompute[i - x]; } if (maxOnes < oneCnt) { maxOnes = oneCnt; } } int swapCnt = x - oneCnt; return swapCnt; } int main() { int arr[] = {1, 0, 1, 1, 0, 1}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Minimum swap count = " << getMinSwaps(arr, n) << endl; return 0; }
When you compile and execute above program. It generates following output −
Output
Minimum swap count = 1
- Related Articles
- Minimum Swaps to Group All 1's Together in Python
- 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
- Minimum swaps required to bring all elements less than or equal to k together in C++
- Program to find minimum number of swaps needed to arrange all pair of socks together in C++
- Minimum swaps required to make a binary string alternating in C++
- Minimum number of swaps required to sort an array in C++
- Program to find minimum swaps required to make given anagram in python
- Program to count number of minimum swaps required to make it palindrome in Python
- Program to group the 1s with minimum number of swaps in a given string in Python
- Minimum Swaps to Make Strings Equal in C++
- Minimum Swaps To Make Sequences Increasing in C++
- Minimum operations required to make all the array elements equal in C++
- Minimum operations required to set all elements of binary matrix in C++
- Minimum number of operations required to sum to binary string S using C++.

Advertisements