Maximum OR value of a pair in an Array without using OR operator | Java implementation of the approach ; 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
import java . util . * ; class GFG { static int maxOR ( int arr [ ] , int n ) { int max_value = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; int number_of_bits = ( int ) ( ( Math . log ( max_value ) ) ) + 2 ; int complement = ( ( 1 << number_of_bits ) - 1 ) ^ max_value ; int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != max_value ) { c = Math . max ( c , ( complement & arr [ i ] ) ) ; } } return ( max_value + c ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 6 , 8 , 16 } ; int n = arr . length ; System . out . print ( maxOR ( arr , n ) ) ; } }