
- 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 make a binary string alternating in C++
Problem statement
Given a binary string of even length and equal number of 0’s and 1’s. What is the minimum number of swaps to make the string alternating? A binary string is alternating if no two consecutive elements are equal
Example
If str = 11110000 then 2 swaps are required.
Algorithm
- Count number of zeroes at odd position and even position of the string. Let their count be oddZeroCnt and evenZeroCnt respectively
- Count number of ones at odd position and even position of the string. Let their count be oddOneCnt and evenOneCnt respectively
- We will always swap a 1 with a 0. So we just check if our alternating string starts with 0 then the number of swaps is min (evenZeroCnt, oddOneCnt) and if our alternating string starts with 1 then the number of swaps is min (evenOneCnt, oddZeroCnt). The answer is min of these two
Example
#include <bits/stdc++.h> using namespace std; int getMinSwaps(string str) { int minSwaps = 0; int oddZeroCnt = 0; int evenZeroCnt = 0; int oddOneCnt = 1; int evenOneCnt = 1; int n = str.length(); for (int i = 0; i < n; ++i) { if (i % 2 == 0) { if (str[i] == '1') { ++evenOneCnt; } else { ++evenZeroCnt; } } else { if (str[i] == '1') { ++oddOneCnt; } else { ++oddZeroCnt; } } } int zeroSwapCnt = min(evenZeroCnt, oddOneCnt); int oneSwapCnt = min(evenOneCnt, oddZeroCnt); return min(zeroSwapCnt, oneSwapCnt); } int main() { string str = "11110000"; cout << "Minimum swaps = " << getMinSwaps(str) << endl; return 0; }
When you compile and execute above program. It generates following output −
Output
Minimum swaps = 2
- Related Articles
- Program to find minimum changes required for alternating binary string in Python
- 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
- Minimum Swaps to Make Strings Equal in C++
- Minimum Swaps To Make Sequences Increasing in C++
- Minimum Swaps required to group all 1’s together in C++
- Minimum number of swaps required to sort an array in C++
- Program to find minimum swaps to arrange a binary grid using Python
- Minimum number of operations required to sum to binary string S using C++.
- Program to find minimum number of flips required to have alternating values in Python
- Minimum swaps required to bring all elements less than or equal to k together in C++
- Program to find minimum number of operations required to make one string substring of other in Python
- Minimum edges required to add to make Euler Circuit in Python
- Minimum edges required to add to make Euler Circuit in C++
- Program to find minimum remove required to make valid parentheses in Python

Advertisements