Maximum OR value of a pair in an Array | Set 2 | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxOR ( int arr [ ] , int n ) { int max_value = * max_element ( arr , arr + n ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans = max ( ans , ( max_value arr [ i ] ) ) ; } return ans ; } int main ( ) { int arr [ ] = { 3 , 6 , 8 , 16 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxOR ( arr , n ) ; return 0 ; }