Minimize K whose XOR with given array elements leaves array unchanged | Function to find the minimum value of K in given range ; Declare a set ; Initialize count variable ; Iterate in range [ 1 , 1024 ] ; counter set as 0 ; Iterating through the Set ; Check if the XOR calculated is present in the Set ; If the value of Bitwise XOR inside the given set then increment count ; Check if the value of count is equal to the size of set ; Return minimum value of K ; If no such K is found ; Given array ; Function Call
function min_value ( arr , N ) { let x , X , K ; let S = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { S . push ( arr [ i ] ) ; } let count = 0 ; for ( let i = 1 ; i <= 1024 ; i ++ ) { count = 0 ; for ( let it in S ) { X = ( ( i it ) - ( i & it ) ) ; for ( let j in S ) { if ( S [ j ] == X ) { count ++ ; } } } if ( count == S . length ) { K = i ; return K ; } } return - 1 ; } let arr = [ 1 , 0 , 3 , 3 , 0 , 2 ] ; let N = arr . length ; document . write ( min_value ( arr , N ) ) ;