Maximum number of 0 s placed consecutively at the start and end in any rotation of a Binary String | Java program for the above approach ; Function to find the maximum sum of consecutive 0 s present at the start and end of a string present in any of the rotations of the given string ; Check if all the characters in the string are 0 ; Iterate over characters of the string ; If the frequency of '1' is 0 ; Print n as the result ; Concatenate the string with itself ; Stores the required result ; Generate all rotations of the string ; Store the number of consecutive 0 s at the start and end of the string ; Count 0 s present at the start ; Count 0 s present at the end ; Calculate the sum ; Update the overall maximum sum ; Print the result ; Driver Code ; Given string ; Store the size of the string
import java . util . * ; class GFG { static void findMaximumZeros ( String str , int n ) { int c0 = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( str . charAt ( i ) == '0' ) c0 ++ ; } if ( c0 == n ) { System . out . print ( n ) ; return ; } String s = str + str ; int mx = 0 ; for ( int i = 0 ; i < n ; ++ i ) { int cs = 0 ; int ce = 0 ; for ( int j = i ; j < i + n ; ++ j ) { if ( s . charAt ( j ) == '0' ) cs ++ ; else break ; } for ( int j = i + n - 1 ; j >= i ; -- j ) { if ( s . charAt ( j ) == '0' ) ce ++ ; else break ; } int val = cs + ce ; mx = Math . max ( val , mx ) ; } System . out . print ( mx ) ; } public static void main ( String [ ] args ) { String s = "1001" ; int n = s . length ( ) ; findMaximumZeros ( s , n ) ; } }