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
def minSteps ( arr , n , k ) : NEW_LINE INDENT cnt = 0 NEW_LINE if ( k == 1 ) : NEW_LINE INDENT flag = 0 NEW_LINE DEDENT else : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == flag ) : NEW_LINE INDENT cnt += 1 NEW_LINE flag = ( flag + 1 ) % 2 NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT arr = [ 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE k = 1 NEW_LINE print ( minSteps ( arr , n , k ) ) NEW_LINE