Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Maximum length of a substring required to be flipped repeatedly to make all characters of binary string equal to 0

  • Difficulty Level : Hard
  • Last Updated : 12 Aug, 2021

Given a binary string S, the task is to find the maximum length of substrings required to be flipped repeatedly to make all the characters of a binary string equal to ‘0’.

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: S = “010”
Output: 2
Explanation:
Following are the order of flipping of substring of  at least K for the value of K as 2:



  • Flip the substring S[0, 2] of length 3(>= K) modifies the string S to “101”.
  • Flip the substring S[0, 1] of length 2(>= K) modifies the string S to “011”.
  • Flip the substring S[1, 2] of length 2(>= K) modifies the string S to “000”.

For the value of K as 2(which is the maximum possible value) all the characters of the string can be made 0. Therefore, print 2.

Input: S = “00001111”
Output: 4

Approach: The given problem can be solved by traversing the given string S, now if at any point the adjacent characters are not the same then flip one sub-string LHS or RHS. For that, take the maximum length of LHS and RHS. There can be multiple adjacent places where characters are not equal. For each pair of substrings, the maximum required will be different. Now to change all the characters to ‘0’ take the minimum among all those maximums. Follow the steps below to solve the problem:

  • Initialize the variable, say ans as N that stores the maximum possible value of K.
  • Iterate over the range [0, N – 1) using the variable i and perform the following steps:
    • If the value of s[i] and s[i + 1] are not the same, then update the value of ans to the minimum of ans or maximum of (i + 1) or (N – i – 1).
  • After performing the above steps, print the value of ans as the result.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the maximum value of K
// such that flipping substrings of size
// at least K make all characters 0s
int maximumK(string& S)
{
    int N = S.length();
 
    // Stores the maximum value of K
    int ans = N;
 
    int flag = 0;
 
    // Traverse the given string S
    for (int i = 0; i < N - 1; i++) {
 
        // Store the minimum of the
        // maximum of LHS and RHS length
        if (S[i] != S[i + 1]) {
 
            // Flip performed
            flag = 1;
            ans = min(ans, max(i + 1,
                               N - i - 1));
        }
    }
 
    // If no flips performed
    if (flag == 0)
        return 0;
 
    // Return the possible value of K
    return ans;
}
 
// Driver Code
int main()
{
    string S = "010";
    cout << maximumK(S);
 
    return 0;
}

Java




// Java code for above approach
import java.util.*;
 
class GFG{
 
// Function to find the maximum value of K
// such that flipping substrings of size
// at least K make all characters 0s
static int maximumK(String S)
{
    int N = S.length();
 
    // Stores the maximum value of K
    int ans = N;
 
    int flag = 0;
 
    // Traverse the given string S
    for (int i = 0; i < N - 1; i++) {
 
        // Store the minimum of the
        // maximum of LHS and RHS length
        if (S.charAt(i) != S.charAt(i + 1)) {
 
            // Flip performed
            flag = 1;
            ans = Math.min(ans, Math.max(i + 1,
                               N - i - 1));
        }
    }
 
    // If no flips performed
    if (flag == 0)
        return 0;
 
    // Return the possible value of K
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    String S = "010";
    System.out.print(maximumK(S));
}
}
 
// This code is contributed by target_2.

Python3




# Python 3 program for the above approach
 
# Function to find the maximum value of K
# such that flipping substrings of size
# at least K make all characters 0s
def maximumK(S):
    N = len(S)
 
    # Stores the maximum value of K
    ans = N
 
    flag = 0
 
    # Traverse the given string S
    for i in range(N - 1):
       
        # Store the minimum of the
        # maximum of LHS and RHS length
        if (S[i] != S[i + 1]):
 
            # Flip performed
            flag = 1
            ans = min(ans, max(i + 1,N - i - 1))
 
    # If no flips performed
    if (flag == 0):
        return 0
 
    # Return the possible value of K
    return ans
 
# Driver Code
if __name__ == '__main__':
    S = "010"
    print(maximumK(S))
     
    # This code is contributed by SURENDRA_GANGWAR.

C#




// C# code for the above approach
using System;
 
public class GFG {
    // Function to find the maximum value of K
    // such that flipping substrings of size
    // at least K make all characters 0s
    static int maximumK(String S)
    {
        int N = S.Length;
 
        // Stores the maximum value of K
        int ans = N;
 
        int flag = 0;
 
        // Traverse the given string S
        for (int i = 0; i < N - 1; i++) {
 
            // Store the minimum of the
            // maximum of LHS and RHS length
            if (S[i] != S[i + 1]) {
 
                // Flip performed
                flag = 1;
                ans = Math.Min(ans,
                               Math.Max(i + 1, N - i - 1));
            }
        }
 
        // If no flips performed
        if (flag == 0)
            return 0;
 
        // Return the possible value of K
        return ans;
    }
 
    // Driver Code
 
    static public void Main()
    {
 
        // Code
        string S = "010";
        Console.Write(maximumK(S));
    }
}
// This code is contributed by Potta Lokesh

Javascript




<script>
// Javascript program for the above approach
 
// Function to find the maximum value of K
// such that flipping substrings of size
// at least K make all characters 0s
function maximumK(S)
{
  let N = S.length;
 
  // Stores the maximum value of K
  let ans = N;
  let flag = 0;
 
  // Traverse the given string S
  for (let i = 0; i < N - 1; i++)
  {
   
    // Store the minimum of the
    // maximum of LHS and RHS length
    if (S[i] != S[i + 1])
    {
     
      // Flip performed
      flag = 1;
      ans = Math.min(ans, Math.max(i + 1, N - i - 1));
    }
  }
 
  // If no flips performed
  if (flag == 0) return 0;
 
  // Return the possible value of K
  return ans;
}
 
// Driver Code
let S = "010";
document.write(maximumK(S));
 
// This code is contributed by gfgking.
</script>
Output: 
2

 

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :