text: Minimum flips required to generate continuous substrings of 0 â €™ s and 1 â €™ s | C # 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
code: using System ; class GFG { static int minChanges ( String str , int N ) { int res ; int count0 = 0 , count1 = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == '0' ) count0 ++ ; } res = count0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == '0' ) count0 -- ; if ( str [ i ] == '1' ) count1 ++ ; res = Math . Min ( res , count1 + count0 ) ; } return res ; } public static void Main ( ) { int N = 9 ; String str = "000101001" ; Console . Write ( minChanges ( str , N ) ) ; } }