Minimum number of flips with rotation to make binary string alternating | Function that finds the minimum number of flips to make the binary string alternating if left circular rotation is allowed ; Initialize prefix arrays to store number of changes required to put 1 s at either even or odd position ; If i is odd ; Update the oddone and evenone count ; Else i is even ; Update the oddone and evenone count ; Initialize minimum flips ; Check if substring [ 0 , i ] is appended at end how many changes will be required ; Return minimum flips ; Given String ; Length of given string ; Function call
function MinimumFlips ( s , n ) { let a = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { a [ i ] = ( s [ i ] == ' ' ? 1 : 0 ) ; } let oddone = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; let evenone = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; oddone [ 0 ] = 0 ; evenone [ 0 ] = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( i % 2 != 0 ) { oddone [ i + 1 ] = oddone [ i ] + ( a [ i ] == 1 ? 1 : 0 ) ; evenone [ i + 1 ] = evenone [ i ] + ( a [ i ] == 0 ? 1 : 0 ) ; } else { oddone [ i + 1 ] = oddone [ i ] + ( a [ i ] == 0 ? 1 : 0 ) ; evenone [ i + 1 ] = evenone [ i ] + ( a [ i ] == 1 ? 1 : 0 ) ; } } let minimum = Math . min ( oddone [ n ] , evenone [ n ] ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( n % 2 != 0 ) { minimum = Math . min ( minimum , oddone [ n ] - oddone [ i + 1 ] + evenone [ i + 1 ] ) ; minimum = Math . min ( minimum , evenone [ n ] - evenone [ i + 1 ] + oddone [ i + 1 ] ) ; } } return minimum ; } let S = " " ; let n = S . length ; document . write ( MinimumFlips ( S , n ) ) ;