Thursday, February 2, 2023
Home TOP Minimize bit flips so that each continuous segment of the same bit...

Minimize bit flips so that each continuous segment of the same bit has an even length

Given a binary string S with an even size containing only 0 seconds and 1s, the task is to find the minimum flips (i.e. 0 to 1 or 1 to 0) such that each contiguous subsegment containing the same bits is an even size.


Enter: S = “1110011000”
Output: 3
Explanation: Change S[2]S[5] and S[6] to ‘0’.
After that S becomes “110000000000”, can be divided into
“11” and “00000000”, which have lengths of 2 and 8, respectively.
There are other ways to operate 3 times like
“1111110000”, “1100001100”, “1111001100”.

Enter: 100110
Output: 3
Explanation: The given string can be converted to 000000
with 3 operations or 111111.

Approach: To solve the problem follow the observations below:

It can be said that we have to split the string into many contiguous binaries of length 2 where the string will be either 00 or 11. So we only need to take care of the order in which s[i] != s[i+1]

  • Now to find the minimum order of operations like “1011” must be changed to “1111” and “0100” must be changed to “0000”.
  • So, the minimum number of operations is 1. Therefore, after we make s[i] = s[i + 1] we will move i to i+2 to check if they are equal or not.

Follow the steps mentioned below to implement the idea:

  • Repeat from i = 0 to N-1:
    • Check if S[i] together with S[i+1].
    • If they are the same then there is no need for a slight flip.
    • If not, increase the number of flips by 1.
    • After each check moved from I to me+2.
  • The total number of flips counted is the minimum answer required.

Below is the implementation of the above approach:



#include <bits/stdc++.h>

using namespace std;


int solve(string s)



    int ans = 0;


    int n = s.size();



    for (int i = 0; i < n; i += 2) {

        if (s[i] != s[i + 1])





    return ans;



int main()


    string S = "1110011000";



    cout << solve(S);

    return 0;


Time complexity: ON)
Help Room: O(1)


Real Madrid vs Girona Football Prediction 30 October 2022 Accurate

Real Madrid Vs Girona Football Prediction October 30, 2022 Accurate And Accurate Tonight with us Vegas99bet as an accurate and reliable football prediction website,...

Anwar said if you are a close friend of Arafat, why don’t you retract the pro-Israel statement

Bersatu Anwar Ibrahim again urged to clarify his position on Israel, saying that the PKK chief’s recent claim to be “Malaysia’s number one fighter...

Download Idle High School Tycoon Mod Apk Unlimited Money

Please download the game Idle High School Tycoon Apk Mod the latest version 2022 is here, because the mod version that we share has...


Please enter your comment!
Please enter your name here

Most Popular

Advantages of business trip massage is practice of massage therapy is an alternative therapy that is quickly gaining popularity. In recent years, the...

Best Sports Toto

Totowiki is formed by a fixed refund rate method and a fixed dividend rate method. The method of fixed refund rate is called 'Toto',...

Uses of Digital Pressure Regulator

In a short time, these regulators have become very popular. They provide flexibility, robustness and have the capability to handle high pressure electronically. Before...

part-time job night part-time job

Chipmakers are beginning to target job cuts and other adjustments, including reducing factory hours to cope with the economic downturn, chief executive...

Recent Comments