Maximum OR value of a pair in an Array without using OR operator | Function to return the maximum bitwise OR for any pair of the given array without using bitwise OR operation ; find maximum element in the array ; finding complement will set all unset bits in a number ; iterate through all other array elements to find maximum AND value ; c will give the maximum value that could be added to max_value to produce maximum OR value ; Driver code
function maxOR ( arr , n ) { var max_value = Math . max . apply ( Math , arr ) ; var number_of_bits = parseInt ( ( ( Math . log ( max_value ) ) ) ) + 2 ; var complement = ( ( 1 << number_of_bits ) - 1 ) ^ max_value ; var c = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != max_value ) { c = Math . max ( c , ( complement & arr [ i ] ) ) ; } } return ( max_value + c ) ; } var arr = [ 3 , 6 , 8 , 16 ] ; var n = arr . length ; document . write ( maxOR ( arr , n ) ) ;