Minimum Subarray flips required to convert all elements of a Binary Array to K | Function to count the minimum number of subarray flips required ; Iterate the array ; If arr [ i ] and flag are equal ; Return the answer ; Driver Code
function minSteps ( arr , n , k ) { let i , cnt = 0 ; let flag ; if ( k == 1 ) flag = 0 ; else flag = 1 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == flag ) { cnt ++ ; flag = ( flag + 1 ) % 2 ; } } return cnt ; } let arr = [ 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 ] ; let n = arr . length ; let k = 1 ; document . write ( minSteps ( arr , n , k ) ) ;