Choose an integer K such that maximum of the xor values of K with all Array elements is minimized | 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
function calculate ( section , pos ) { if ( pos < 0 ) return 0 ; let on_section = [ ] , off_section = [ ] ; for ( let el = 0 ; el < section . length ; el ++ ) { if ( ( ( section [ el ] >> pos ) & 1 ) == 0 ) off_section . push ( section [ el ] ) ; else on_section . push ( section [ el ] ) ; } if ( off_section . length == 0 ) return calculate ( on_section , pos - 1 ) ; if ( on_section . length == 0 ) return calculate ( off_section , pos - 1 ) ; return Math . min ( calculate ( off_section , pos - 1 ) , calculate ( on_section , pos - 1 ) ) + ( 1 << pos ) ; } function minXorValue ( a , n ) { let section = [ ] ; for ( let i = 0 ; i < n ; i ++ ) section . push ( a [ i ] ) ; return calculate ( section , 30 ) ; } let N = 4 ; let A = [ 3 , 2 , 5 , 6 ] ; document . write ( minXorValue ( A , N ) ) ;