Maximum OR value of a pair in an Array | Set 2 | Java implementation of the approach above ; Function to return the maximum bitwise OR for any pair of the given array in O ( n ) time complexity . ; Find the maximum element in the array ; Stores the maximum OR value ; Traverse the array and perform Bitwise OR between every array element with the maximum element ; Driver Code
import java . util . Arrays ; class GFG { static int maxOR ( int [ ] arr , int n ) { int max_value = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans = Math . max ( ans , ( max_value arr [ i ] ) ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 3 , 6 , 8 , 16 } ; int n = 4 ; System . out . print ( maxOR ( arr , n ) ) ; } }