Maximum length of a substring required to be flipped repeatedly to make all characters of binary string equal to 0
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:
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>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Minimize flips to make binary string as all 1s by flipping characters in substring of size K repeatedly Given a binary string S of size N and an integer K, the task is to find the minimum number of operations required to make all characters as 1s in the binary string by flipping characters in the substring of size K. If it is not possible to do so, then print "-1". Examples: Input: S = "00010110 ", K
7 min read
Minimize cost to make all characters of a Binary String equal to '1' by reversing or flipping characters of substrings Given a binary string S, and two integers A, which denotes the cost of reversing a substring, and B, which denotes the cost of flipping all characters of a substring, the task is to find the minimum cost to reduce the string S to 1s only. Examples: Input: S = "01100", A = 1, B = 5Output: 6Explanatio
8 min read
Minimum cost of flipping characters required to convert Binary String to 0s only Given binary string str, and integers A, which denotes the cost of converting consecutive 1s to 0s, and B, which denotes the cost of converting 0s to 1s. The task is to find the minimum cost to reduce the string str to 0s only. Examples: Input: str = â01101110â, A = 5, B = 1Output: 6Explanation:Conv
8 min read
Minimum flips required to convert given string into concatenation of equal substrings of length K Given a binary string S and an integer K, the task is to find the minimum number of flips required to convert the given string into a concatenation of K-length equal sub-strings. It is given that the given string can be split into K-length substrings. Examples: Input: S = "101100101", K = 3 Output:
5 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 substrin
11 min read
Minimum shifts of substrings of 1s required to group all 1s together in a given Binary string Given a binary string S of length N, the task is to print the minimum number of indices, substrings consisting only of 1s are required to be shifted such that all 1s present in the string are grouped together. Examples: Input: S = "00110111011"Output: 2Explanation: Operation 1: Shift substring {S[2]
6 min read