Minimize K whose XOR with given array elements leaves array unchanged | Java program for the above approach ; 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 ; Driver Code ; Given array ; Function Call
import java . util . * ; class GFG { static int min_value ( int arr [ ] , int N ) { int x , X , K ; HashSet < Integer > S = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { S . add ( arr [ i ] ) ; } int count = 0 ; for ( int i = 1 ; i <= 1024 ; i ++ ) { count = 0 ; for ( int it : S ) { X = ( ( i it ) - ( i & it ) ) ; if ( S . contains ( X ) ) { count ++ ; } } if ( count == S . size ( ) ) { K = i ; return K ; } } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 , 3 , 3 , 0 , 2 } ; int N = arr . length ; System . out . print ( min_value ( arr , N ) ) ; } }