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