Choose an integer K such that maximum of the xor values of K with all Array elements is minimized | Java implementation to find Minimum possible value of the maximum xor in an array by choosing some integer ; Function to calculate Minimum possible value of the Maximum XOR in an array ; Base case ; Divide elements into two sections ; Traverse all elements of current section and divide in two groups ; Check if one of the sections is empty ; Explore both the possibilities using recursion ; Function to calculate minimum XOR value ; Start recursion from the most significant pos position ; Driver code
import java . util . * ; class GFG { static int calculate ( Vector < Integer > section , int pos ) { if ( pos < 0 ) return 0 ; Vector < Integer > on_section = new Vector < Integer > ( ) , off_section = new Vector < Integer > ( ) ; for ( int el : section ) { if ( ( ( el >> pos ) & 1 ) == 0 ) off_section . add ( el ) ; else on_section . add ( el ) ; } if ( off_section . size ( ) == 0 ) return calculate ( on_section , pos - 1 ) ; if ( on_section . size ( ) == 0 ) return calculate ( off_section , pos - 1 ) ; return Math . min ( calculate ( off_section , pos - 1 ) , calculate ( on_section , pos - 1 ) ) + ( 1 << pos ) ; } static int minXorValue ( int a [ ] , int n ) { Vector < Integer > section = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) section . add ( a [ i ] ) ; return calculate ( section , 30 ) ; } public static void main ( String [ ] args ) { int N = 4 ; int A [ ] = { 3 , 2 , 5 , 6 } ; System . out . print ( minXorValue ( A , N ) ) ; } }