Open In App

Minimum flips required to form given binary string where every flip changes all bits to its right as well

Last Updated : 13 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string S, the task is to find minimum flips required to convert an initial binary string consisting of only zeroes to S where every flip of a character flips all succeeding characters as well. 
Examples: 
 

Input: S = “01011” 
Output:
Explanation: 
Initial String – “00000” 
Flip the 2nd bit – “01111” 
Flip the 3rd bit – “01000” 
Flip the 4th bit – “01011” 
Total Flips = 3
Input: S = “01001” 
Output:
Explanation: 
Initial String – “00000” 
Flip the 2nd bit – “01111” 
Flip the 3rd bit – “01000” 
Flip the 5th bit – “01001” 
Total Flips = 3 
 

 

Approach: 
In order to solve the problem, follow the steps below: 
 

  • Store ‘1’ in curr initially.
  • Traverse S and find the first occurrence of curr. Increase count when curr is encountered. Store ‘0’ if curr is ‘1’ or vice versa.
  • Repeat the above step for entire traversal of S.
  • Final value of count gives the required answer.

Below is the implementation of the above approach.

C++




// C++ program for the above approach
#include<bits/stdc++.h>
using namespace std;
 
// Function to return the count
// of minimum flips required
int minFlips(string target)
{
    char curr = '1';
    int count = 0;
    for(int i = 0; i < target.length(); i++)
    {
         
       // If curr occurs in the final string
       if (target[i] == curr)
       {
           count++;
            
           // Switch curr to '0' if '1'
           // or vice-versa
           curr = (char)(48 + (curr + 1) % 2);
       }
    }
    return count;
}
 
// Driver Code
int main()
{
    string S = "011000";
     
    cout << (minFlips(S));
}
 
// This code is contributed by rock_cool


Java




// Java program for the above approach
import java.util.Arrays;
 
public class GFG {
    // Function to return the count of
    // minimum flips required
    public static int minFlips(String target)
    {
 
        char curr = '1';
        int count = 0;
        for (int i = 0; i < target.length(); i++) {
 
            // If curr occurs in the final string
            if (target.charAt(i) == curr) {
 
                count++;
 
                // Switch curr to '0' if '1'
                // or vice-versa
                curr = (char)(48 + (curr + 1) % 2);
            }
        }
 
        return count;
    }
 
    // Driver Code
    public static void main(String args[])
    {
 
        String S = "011000";
        System.out.println(minFlips(S));
    }
}


Python3




# Python3 program for the above approach
 
# Function to return the count
# of minimum flips required
def minFlips(target):
 
    curr = '1'
    count = 0
     
    for i in range(len(target)):
         
        # If curr occurs in the final string
        if (target[i] == curr):
            count += 1
             
            # Switch curr to '0' if '1'
            # or vice-versa
            curr = chr(48 + (ord(curr) + 1) % 2)
     
    return count
 
# Driver Code
if __name__ == "__main__":
     
    S = "011000"
     
    print(minFlips(S))
 
# This code is contributed by chitranayal


C#




// C# program for the above approach
using System;
 
class GFG{
     
// Function to return the count of
// minimum flips required
public static int minFlips(String target)
{
    char curr = '1';
    int count = 0;
    for(int i = 0; i < target.Length; i++)
    {
         
       // If curr occurs in the readonly string
       if (target[i] == curr)
       {
           count++;
            
           // Switch curr to '0' if '1'
           // or vice-versa
           curr = (char)(48 + (curr + 1) % 2);
       }
    }
    return count;
}
 
// Driver code
public static void Main(String []args)
{
    String S = "011000";
    Console.WriteLine(minFlips(S));
}
}
 
// This code is contributed by 29AjayKumar


Javascript




<script>
 
    // Javascript program for the above approach
     
    // Function to return the count
    // of minimum flips required
    function minFlips(target)
    {
        let curr = '1';
        let count = 0;
        for(let i = 0; i < target.length; i++)
        {
 
           // If curr occurs in the final string
           if (target[i] == curr)
           {
               count++;
 
               // Switch curr to '0' if '1'
               // or vice-versa
               curr = String.fromCharCode(48 + (curr.charCodeAt() + 1) % 2);
           }
        }
        return count;
    }
     
    let S = "011000";
       
    document.write(minFlips(S));
     
</script>


Output: 

2

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



Similar Reads

Minimum number of flips required such that a Binary Matrix doesn't contain any path from the top left to the bottom right consisting only of 0s
Given a binary matrix mat[][] of dimensions N*M, the task is to find the minimum number of flips required from the given binary matrix such that there doesn't exist any path from the top-left cell to the bottom-right cell consisting of only 0s. Examples: Input: mat[][] = {{0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}}Output: 1Explanation:O
11 min read
Minimize flips required to make all shortest paths from top-left to bottom-right of a binary matrix equal to S
Given a binary matrix mat[][] having dimensions N * M and a binary string S of length N + M - 1 , the task is to find the minimum number of flips required to make all shortest paths from the top-left cell to the bottom-right cell equal to the given string S. Examples: Input: mat[][] = [[1, 0, 1, 1], [1, 1, 1, 0]], S = "10010"Output: 3 Explanation:
6 min read
Minimum flips required to keep all 1s together in a Binary string
Given binary string str, the task is to find the minimum number of flips required to keep all 1s together in the given binary string, i.e. there must not be any 0 between 1s in the string. Examples: Input: str = "0011111100" Output: 0 Explanation: We dont need to flip any bits because all the ones are grouped together and there is no zero between a
8 min read
Minimum flips required in a binary string such that all K-size substring contains 1
Given a binary string str of size N and a positive integer K, the task is to find the minimum number of flips required to make all substring of size K contain at least one '1'.Examples: Input: str = "0001", K = 2 Output: 1 Explanation: Flipping the bit at index 1 modifies str to "0101". All substrings of size 2 are "01", "10", and "01". Each substr
11 min read
Minimum non-adjacent pair flips required to remove all 0s from a Binary String
The problem statement asks us to find the minimum number of operations required to remove all 0s from a given binary string S. An operation involves flipping at most two non-adjacent characters (i.e., characters that are not next to each other) in the string. Let's take the first example given: S = "110010". The initial string contains three 0s at
11 min read
Minimum bit flips such that every K consecutive bits contain at least one set bit
Given a binary string S, and an integer K, the task is to find the minimum number of flips required such that every substring of length K contains at least one '1'.Examples: Input: S = "10000001" K = 2 Output: 3 Explanation: We need only 3 changes in string S ( at position 2, 4 and 6 ) so that the string contain at least one '1' in every sub-string
8 min read
Flip bits of the sum of count of set bits of two given numbers
Given two numbers A and B, the task is to count the number of set bits in A and B and flip the bits of the obtained sum. Examples: Input: A = 5, B = 7Output: 2Explanation:Binary representation of A is 101.Binary representation of B is 111.Count of set bits in A and B = 2 + 3 = 5.Binary representation of the sum obtained = 101Flipping the bits of th
5 min read
Minimum substring flips required to convert given binary string to another
Given two binary strings A and B, the task is to find the minimum number of times a substring starting from the first character of A needs to be flipped, i.e. convert 1s to 0s and 0s to 1s, to convert A to B. Examples: Input: A = “0010”, B = “1011”Output; 3Explanation:Step 1: Flip the entire string A. Therefore, A becomes “1101” .Step 2: Flip the s
7 min read
Minimum flips required to maximize a number with k set bits
Given two numbers n and k, we need to find the minimum number of flips required to maximize given number by flipping its bits such that the resulting number has exactly k set bits. Note : K must be less than number of bits in n.Examples : Input : n = 14, k = 2 Output : Min Flips = 1 Explanation : Binary representation of 14 = 1110 Largest 4-digit B
7 min read
Minimize flips on adjacent 2 to 3 bits to generate a binary string of all 1s
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. Examples: Input: S = "0010"Output: 2Explanation: Operations performed are: 0010 -&gt; 0001 -&gt; 1111 Input: S = "0011110"Output: 3Explana
7 min read