Skip to content
Related Articles

Related Articles

Minimize count of flips required such that no substring of 0s have length exceeding K
  • Difficulty Level : Medium
  • Last Updated : 04 Sep, 2020

Given a binary string str of length N and an integer K where K is in the range (1 ≤ K ≤ N), the task is to find the minimum number of flips( conversion of 0s to 1 or vice versa) required to be performed on the given string such that the resulting string does not contain K or more zeros together.

Examples:

Input: str = “11100000011”, K = 3 
Output:
Explanation: 
Flipping 6th and 7th characters modifies the string to “11100110011”. 
Therefore, no substring of 0s are present in the string having length 3 or more.

Input: str = “110011”, K = 1 
Output:
Flip 3rd and 4th characters then str = “111111” which does not contain 1 or more zeros together.

Naive Approach: The simplest approach is to generate all possible substrings of the given string and for each substring, check if it consists of only 0s or not. If found to be true, check if its length exceeds K or not. If found to be true, count the number of flips required for that substring. Finally, print the count of flips obtained.



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

Efficient Approach: To make the count of contiguous zeros in resultant string less than K, choose the substrings consisting of only zeros of length ≥ K. So the problem reduces to finding the minimum flips in each substring. The final result will be the sum of minimum flips of all such subsegments. Below are the steps:

  1. Initialize the result to 0and the count of contiguous zeros (say cnt_zeros) to 0.
  2. Transverse the string and on encountering a ‘0’ increment cnt_zeros by 1.
  3. If cnt_zeros becomes equal to K then increment result and set cnt_zeros to 0.
  4. And when a ‘1’ comes then set cnt_zeros to 0 since the count of contiguous zeros should become 0.

Below the implementation of the above approach:

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return minimum
// number of flips required
int min_flips(string& str, int k)
{
    // Base Case
    if (str.size() == 0)
        return 0;
  
    // Stores the count of
    // minimum number of flips
    int ans = 0;
  
    // Stores the count of zeros
    // in current substring
    int cnt_zeros = 0;
  
    for (char ch : str) {
  
        // If current character is 0
        if (ch == '0') {
  
            // Continue ongoing
            // substring
            ++cnt_zeros;
        }
        else {
  
            // Start a new substring
            cnt_zeros = 0;
        }
  
        // If k consecutive
        // zeroes are obtained
        if (cnt_zeros == k) {
            ++ans;
  
            // End segment
            cnt_zeros = 0;
        }
    }
  
    // Return the result
    return ans;
}
  
// Driver Code
int main()
{
    string str = "11100000011";
    int k = 3;
  
    // Function call
    cout << min_flips(str, k);
    return 0;
}

Java




// Java program to implement
// the above approach
class GFG{
  
// Function to return minimum
// number of flips required
static int min_flips(String str, int k)
{
      
    // Base Case
    if (str.length() == 0)
        return 0;
  
    // Stores the count of
    // minimum number of flips
    int ans = 0;
  
    // Stores the count of zeros
    // in current subString
    int cnt_zeros = 0;
  
    for(char ch : str.toCharArray())
    {
          
        // If current character is 0
        if (ch == '0')
        {
              
            // Continue ongoing
            // subString
            ++cnt_zeros;
        }
        else 
        {
              
            // Start a new subString
            cnt_zeros = 0;
        }
  
        // If k consecutive
        // zeroes are obtained
        if (cnt_zeros == k)
        {
            ++ans;
              
            // End segment
            cnt_zeros = 0;
        }
    }
  
    // Return the result
    return ans;
}
  
// Driver Code
public static void main(String[] args)
{
    String str = "11100000011";
    int k = 3;
  
    // Function call
    System.out.print(min_flips(str, k));
}
}
  
// This code is contributed by 29AjayKumar

Python3




# Python3 program to implement
# the above approach
  
# Function to return minimum
# number of flips required
def min_flips(strr, k):
      
    # Base Case
    if (len(strr) == 0):
        return 0
  
    # Stores the count of
    # minimum number of flips
    ans = 0
  
    # Stores the count of zeros
    # in current sub
    cnt_zeros = 0
  
    for ch in strr:
  
        # If current character is 0
        if (ch == '0'):
  
            # Continue ongoing
            # sub
            cnt_zeros += 1
        else:
  
            # Start a new sub
            cnt_zeros = 0
  
        # If k consecutive
        # zeroes are obtained
        if (cnt_zeros == k):
            ans += 1
  
            # End segment
            cnt_zeros = 0
  
    # Return the result
    return ans
  
# Driver Code
if __name__ == '__main__':
      
    strr = "11100000011"
    k = 3
  
    # Function call
    print(min_flips(strr, k))
  
# This code is contributed by mohit kumar 29

C#




// C# program to implement
// the above approach
using System;
  
class GFG{
  
// Function to return minimum
// number of flips required
static int min_flips(String str, int k)
{
      
    // Base Case
    if (str.Length == 0)
        return 0;
  
    // Stores the count of
    // minimum number of flips
    int ans = 0;
  
    // Stores the count of zeros
    // in current subString
    int cnt_zeros = 0;
  
    foreach(char ch in str.ToCharArray())
    {
          
        // If current character is 0
        if (ch == '0')
        {
              
            // Continue ongoing
            // subString
            ++cnt_zeros;
        }
        else
        {
              
            // Start a new subString
            cnt_zeros = 0;
        }
  
        // If k consecutive
        // zeroes are obtained
        if (cnt_zeros == k)
        {
            ++ans;
              
            // End segment
            cnt_zeros = 0;
        }
    }
  
    // Return the result
    return ans;
}
  
// Driver Code
public static void Main(String[] args)
{
    String str = "11100000011";
    int k = 3;
  
    // Function call
    Console.Write(min_flips(str, k));
}
}
  
// This code is contributed by 29AjayKumar 
Output: 
2

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :