
- 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
Number of flips to make binary string alternate - Set 1 in C++
Let's say you have given a binary string "10011". To make an alternate binary string, we need to flip a minimum of 2 characters as "10101".
There are two possibilities for the alternate string. It will start with 0 or 1. We will check for two alternates and count the number of flips required for both.
And then return the minimum of both. Let's see an example.
Input
binary = "10011"
Output
2
If we start the string with 0, then we have to flip 3 times. And if we start the string with 1, then we have to flip 2 times. The minimum is 2.
Algorithm
- Initialise the binary string.
- Count the flips required to make string alternate starting with 1.
- Similarly count the flips required to make string alternate starting with 0.
- Find the minimum from the above two.
- Print the menimum.
Implementation
Following is the implementation of the above algorithm in C++
#include <bits/stdc++.h> using namespace std; char flip(char binaryDigit) { return binaryDigit == '0' ? '1' : '0'; } int getFlipCountToAlternateString(string binary, char expected) { int flipCount = 0; for (int i = 0; i < binary.length(); i++) { if (binary[i] != expected) { flipCount++; } expected = flip(expected); } return flipCount; } int main() { string binary = "10011"; cout << min(getFlipCountToAlternateString(binary, '0'), getFlipCountToAlternateString(binary, '1')) << endl; return 0; }
Output
If you run the above code, then you will get the following result.
2
- Related Articles
- Finding minimum flips in a binary string using JavaScript
- Minimum Number of Flips to Convert Binary Matrix to Zero Matrix in C++
- Check if a number has bits in alternate pattern - Set 1 in C++
- Minimum flips required to maximize a number with k set bits in C++.
- Check if a number has bits in alternate pattern - Set-2 O(1) Approach in C++
- Count of operations to make a binary string “ab” free in C++
- Minimum Flips to Make a OR b Equal to c in C++
- Minimum Number of K Consecutive Bit Flips in C++
- Program to find number of columns flips to get maximum number of equal Rows in Python?
- Minimum swaps required to make a binary string alternating in C++
- Check if it is possible to rearrange a binary string with alternate 0s and 1s in Python
- Prime Number of Set Bits in Binary Representation in C++
- Prime Number of Set Bits in Binary Representation in Python
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue) in C++
- Find the Number of Unique Permutations Starting with 1 of a Binary String using C++

Advertisements