Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Minimize flips on adjacent 2 to 3 bits to generate a binary string of all 1s

  • Difficulty Level : Medium
  • Last Updated : 16 Jan, 2022

Given a binary string S consisting of 0’s and 1’s, The task is to find the minimum number of flips required to generate a binary string of all ones. The flip is performed on either two or three adjacent indices.


Input: S = “0010”
Output: 2
Explanation: Operations performed are: 0010 -> 0001 -> 1111 

Input: S = “0011110”
Output: 3
Explanation: Operations performed are: 0011110 -> 1111110 -> 1111000  -> 1111111


Approach: The basic idea to solve this problem is to perform flips on the first 2 or 3 characters and add the result of recursion with the next positions. It’s just a recursive brute forcing of all possibilities. For every index of the string, try two adjacent flips and three adjacent flips and then recurse on that subproblem. One important thing is that the result is independent of the order of the flips. 
Follow the below steps to solve the problem:

  1. If the given string doesn’t consist of any zeros then return “0” and if there are only “1’s” then no flip is needed.
  2. If the given string, at any moment becomes equal to “00” or “000” then return “1” as only 1 flip is needed to make it all ones.
  3. We need a list for storing and comparing minimum possibilities at any moment of the recursive call.
  4. If the string starts with “0” and the length of the string is greater than 2 or 3 then we flip the first two or first three characters and recursively call again for the next index.
  5. If strings start with “1“, we remove it by recursively calling it for the rest of the part.
  6. Now in the case of “1“, if “0” is present at the first two or three positions then we flip the first two or three positions and recursively call for the rest of the part along with appending/adding results to the lists.
  7. Once a cycle is completed we return the minimum of all the elements stored in the list.

Below is the implementation of the above approach:


# Python program for the above approach
def minimumFlips(string):
    # No flip needed
    if "0" not in string:
        return 0
    # Flip the whole string at once
    if string in ("00", "000"):
        return 1
    # Flip 1st 2 / 3 characters
    def flip(n):
        return string[:n]\
               .translate({48: 49, 49: 48})\
               + string[n:]
    # For tracking minimum flips
    flips = [float('inf')]
    # Flip if string starts with zero
    if string.startswith("0"):
        # Flip first 2 and call recursively
        if len(string) > 2:
            flips.append(1 + minimumFlips(flip(2)[1:]))
        # Flip first 3 and call recursively
        if len(string) > 3:
            flips.append(1 + minimumFlips(flip(3)[1:]))
    # If string starts with "1"
        # No flip + recursion
        # Flip if zero is at 2nd position
        if "0" in string[:2]:
            # Flip first 2 and call recursively
            flips.append(1 + minimumFlips(flip(2)))
        # Flip if zero is at 2nd position
        if "0" in string[:3]:
            # Flip first 3 and call recursively
            flips.append(1 + minimumFlips(flip(3)))
    return min(flips)
# Driver code
if __name__ == "__main__":

Time Complexity: O(N!)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!