Minimize Bitwise XOR of array elements with 1 required to make sum of array at least K | Function to find minimum number of Bitwise XOR of array elements with 1 required to make sum of the array at least K ; Stores the count of even array elements ; Stores sum of the array ; Traverse the array arr [ ] ; Increment sum ; If array element is even ; Increase count of even ; If S is at least K ; If S + E is less than K ; Otherwise , moves = K - S ; Driver Code
def minStepK ( arr , N , K ) : NEW_LINE INDENT E = 0 NEW_LINE S = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT S += arr [ i ] NEW_LINE if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT E += 1 NEW_LINE DEDENT DEDENT if ( S >= K ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif ( S + E < K ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return K - S NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 0 , 1 , 1 , 0 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE K = 4 NEW_LINE print ( minStepK ( arr , N , K ) ) NEW_LINE DEDENT