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
function minStepK ( arr , N , K ) { var E = 0 ; var S = 0 ; for ( var i = 0 ; i < N ; i ++ ) { S += arr [ i ] ; if ( arr [ i ] % 2 === 0 ) E += 1 ; } if ( S >= K ) return 0 ; else if ( S + E < K ) return - 1 ; else return K - S ; } var arr = [ 0 , 1 , 1 , 0 , 1 ] ; var N = arr . length ; var K = 4 ; document . write ( minStepK ( arr , N , K ) ) ;