# Maximum OR value of a pair in an Array without using OR operator

Given an array **arr[]** containing** N **positive integers, the task is to find the maximum bitwise OR value of a pair in the given array without using the Bitwise OR operator.

**Examples:**

Input:arr[] = {3, 6, 8, 16}

Output:24

Explanation:

The pair giving maximum OR value is (8, 16) => 8|16 = 24

Input:arr[] = {8, 7, 3, 12}

Output:15

Explanation:

There are more than one pair giving us the maximum OR value. One among them => 8|7 = 15

**Approach:** The idea is to find the two numbers which have the most count of set bits at distinct indices. In this way, the resultant number will have all those indices as a set bit, and this can be done without using the OR operator.

- Find out the maximum element in the array and then find the particular element in the remaining array that will have the set bit at the indexes where the maximum element has an unset bit.
- To maximize our output we have to find such an element that will have a set bit in such a manner that will maximize our output.
- Calculate the complement of the maximum element in the array and find the maximum AND value with the other numbers.
- Maximum AND value of this compliment with other array elements will give us the maximum unset bits that could be set in our answer due to other array elements.
- Adding maximum elements with this maximum AND value will give us our desired maximum OR value pair without using OR operation.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the maximum bitwise OR ` `// for any pair of the given array ` `// without using bitwise OR operation ` `int` `maxOR(` `int` `arr[], ` `int` `n) ` `{ ` ` ` ` ` `// find maximum element in the array ` ` ` `int` `max_value ` ` ` `= *max_element(arr, arr + n); ` ` ` ` ` `int` `number_of_bits ` ` ` `= ` `floor` `(log2(max_value)) + 1; ` ` ` ` ` `// finding compliment will set ` ` ` `// all unset bits in a number ` ` ` `int` `compliment ` ` ` `= ((1 << number_of_bits) - 1) ` ` ` `^ max_value; ` ` ` ` ` `int` `c = 0; ` ` ` ` ` `// iterate through all other ` ` ` `// array elements to find ` ` ` `// maximum AND value ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(arr[i] != max_value) { ` ` ` `c = max(c, (compliment & arr[i])); ` ` ` `} ` ` ` `} ` ` ` ` ` `// c will give the maximum value ` ` ` `// that could be added to max_value ` ` ` `// to produce maximum OR value ` ` ` `return` `(max_value + c); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 3, 6, 8, 16 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << maxOR(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Pyhton3 implementation of the approach ` `from` `math ` `import` `log2, floor ` ` ` `# Function to return the maximum bitwise OR ` `# for any pair of the given array ` `# without using bitwise OR operation ` `def` `maxOR(arr, n): ` ` ` ` ` `# Find maximum element in the array ` ` ` `max_value ` `=` `max` `(arr) ` ` ` ` ` `number_of_bits ` `=` `floor(log2(max_value)) ` `+` `1` ` ` ` ` `# Finding compliment will set ` ` ` `# all unset bits in a number ` ` ` `compliment ` `=` `(((` `1` `<< number_of_bits) ` `-` `1` `) ^ ` ` ` `max_value) ` ` ` `c ` `=` `0` ` ` ` ` `# Iterate through all other ` ` ` `# array elements to find ` ` ` `# maximum AND value ` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `if` `(arr[i] !` `=` `max_value): ` ` ` `c ` `=` `max` `(c, (compliment & arr[i])) ` ` ` ` ` `# c will give the maximum value ` ` ` `# that could be added to max_value ` ` ` `# to produce maximum OR value ` ` ` `return` `(max_value ` `+` `c) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `arr ` `=` `[` `3` `, ` `6` `, ` `8` `, ` `16` `] ` ` ` `n ` `=` `len` `(arr) ` ` ` ` ` `print` `(maxOR(arr, n)) ` ` ` `# This code is contributed by Bhupendra_Singh ` |

*chevron_right*

*filter_none*

**Output:**

24

**Time Complexity:** O(N)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Maximum OR value of a pair in an Array | Set 2
- Maximum AND value of a pair in an array
- Maximum OR value of a pair in an array
- Find a pair from the given array with maximum nCr value
- Find pair with maximum GCD in an array
- Print pair with maximum AND value in an array
- Find the maximum sum pair in an Array with even parity
- Maximum pair sum in the given index ranges of an Array
- Find pair with maximum ratio in an Array
- Find a pair (n,r) in an integer array such that value of nPr is maximum
- Find a pair (n,r) in an integer array such that value of nCr is maximum
- Find a pair with maximum product in array of Integers
- Probability of choosing a random pair with maximum sum in an array
- Probability that a random pair chosen from an array (a[i], a[j]) has the maximum sum
- Probability of a random pair being the maximum weighted pair
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Pair formation such that maximum pair sum is minimized
- Maximum XOR value of a pair from a range
- Pair with maximum GCD from two arrays
- Pair with given sum and maximum shortest distance from end

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.