
- 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 flips to make all 1s in left and 0s in right in C++
Problem statement
Given a binary string in which we can flip all 1’s in left part and all 0’s in right part. The task is to calculate minimum flips required to make all 1’s in left and all 0’s in right
Example
Given binary string is 0010101. In this string there are 3 1-bits and 4 0-bits. We have to flip highlighted 4 bits to make all 1’s in left and all 0’s in right as shown below −
0010101
After flipping string will become −
1110000
Algorithm
- Traverse the string from left to right and calculate the number of flips required to convert all 0’s to 1’s.
- Traverse the string from right to left and calculate the number of flips required to covert all 1’s to 0’s
- Traverse through all positions between bits and find minimal value of (0’s flips + 1′s flips)
Example
#include <iostream> #include <string> #include <climits> using namespace std; int minFlips(string binaryString) { int n = binaryString.length(); int flipCnt, zeroFlips[n], oneFlips[n]; flipCnt = 0; for (int i = 0; i < n; ++i) { if (binaryString[i] == '0') { ++flipCnt; } zeroFlips[i] = flipCnt; } flipCnt = 0; for (int i = n - 1; i >= 0; --i) { if (binaryString[i] == '1') { ++flipCnt; } oneFlips[i] = flipCnt; } int minFlips = INT_MAX; for (int i = 1; i < n; ++i) { int sum = zeroFlips[i - 1] + oneFlips[i]; if (sum < minFlips) { minFlips = sum; } } return minFlips; } int main() { string binaryString = "0010101"; cout << "Minimum flips: " << minFlips(binaryString) << endl; return 0; }
Output
When you compile and execute above program. It generates following output −
Minimum flips: 4
- Related Articles
- Segregate all 0s on right and 1s on left in JavaScript
- Count minimum right flips to set all values in an array in C++
- Minimum Flips to Make a OR b Equal to c in C++
- Count all 0s which are blocked by 1s in binary matrix in C++
- Print n 0s and m 1s such that no two 0s and no three 1s are together in C Program
- XOR counts of 0s and 1s in binary representation in C++
- Minimum toggles to partition a binary array so that it has first 0s then 1s in C++
- Largest subarray with equal number of 0s and 1s in C++
- Python - List Initialization with alternate 0s and 1s
- Count Substrings with equal number of 0s, 1s and 2s in C++
- Program to find minimum possible sum by changing 0s to 1s k times from a list of numbers in Python?
- Program to find minimum swaps needed to group all 1s together in Python
- Encoding a number string into a string of 0s and 1s in JavaScript
- Minimum 1s to lend power to make whole array powerful using C++
- C Program to construct DFA accepting odd numbers of 0s and 1s

Advertisements