Minimum flips required to generate continuous substrings of 0 â €™ s and 1 â €™ s | Java implementation of the above approach ; Traverse input string and store the count of 0 ; Traverse the input string again to find minimum number of flips ; Driver code
import java . io . * ; class GFG { static int minChanges ( String str , int N ) { int res ; int count0 = 0 , count1 = 0 ; for ( char x : str . toCharArray ( ) ) { if ( x == '0' ) count0 ++ ; } res = count0 ; for ( char x : str . toCharArray ( ) ) { if ( x == '0' ) count0 -- ; if ( x == '1' ) count1 ++ ; res = Math . min ( res , count1 + count0 ) ; } return res ; } public static void main ( String [ ] args ) { int N = 9 ; String str = "000101001" ; System . out . println ( minChanges ( str , N ) ) ; } }