Given an array arr[] of N positive elements, the task is to find the maximum bitwise OR value of a pair from the given array.
Examples:
Input: arr[] = {3, 6, 8, 16}
Output: 24
Explanation:
The pair giving maximum OR value is (8, 16)
8|16 = 24Input: arr[] = {8, 7, 3, 12}
Output: 15
Explanation:
There are more than one pair giving us the maximum OR value.
8|7 = 15
Naive Approach: Refer to Maximum OR value of a pair in an array for the naive approach.
Efficient Approach: In this article, we will discuss an optimized solution for the given problem.
Follow the steps below to solve the problem:
- Find the largest element from the array.
- Perform Bitwise OR between the largest element and the remaining array elements one by one. This is because the arrangement of set bits in the largest element will contribute to maximum OR value possible from array elements.
- Print the maximum OR value obtained performing the above step.
Below is the implementation of the above approach:
C++
// C++ implementation of // the approach above #include <bits/stdc++.h> using namespace std; // Function to return the maximum // bitwise OR for any pair of the // given array in O(n) time complexity. int maxOR(int arr[], int n) { // Find the maximum // element in the array int max_value = *max_element(arr, arr + n); // Stores the maximum // OR value int ans = 0; // Traverse the array and // perform Bitwise OR // between every array element // with the maximum element for (int i = 0; i < n; i++) { ans = max(ans, (max_value | arr[i])); } return ans; } // Driver Code int main() { int arr[] = { 3, 6, 8, 16 }; int n = sizeof(arr) / sizeof(arr[0]); cout << maxOR(arr, n); return 0; } |
Java
// Java implementation of // the approach above import java.util.Arrays; class GFG{ // Function to return the maximum // bitwise OR for any pair of the // given array in O(n) time complexity. static int maxOR(int []arr, int n) { // Find the maximum // element in the array int max_value = Arrays.stream(arr).max().getAsInt(); // Stores the maximum // OR value int ans = 0; // Traverse the array and // perform Bitwise OR // between every array element // with the maximum element for (int i = 0; i < n; i++) { ans = Math.max(ans, (max_value | arr[i])); } return ans; } // Driver Code public static void main(String[] args) { int arr[] = new int[]{ 3, 6, 8, 16 }; int n = 4; System.out.print(maxOR(arr, n)); } } // This code is contributed by Ritik Bansal |
C#
// C# implementation of // the approach above using System; using System.Linq; class GFG{ // Function to return the maximum // bitwise OR for any pair of the // given array in O(n) time complexity. static int maxOR(int []arr, int n) { // Find the maximum // element in the array int max_value = arr.Max(); // Stores the maximum // OR value int ans = 0; // Traverse the array and // perform Bitwise OR // between every array element // with the maximum element for(int i = 0; i < n; i++) { ans = Math.Max(ans, (max_value | arr[i])); } return ans; } // Driver Code public static void Main(String[] args) { int []arr = { 3, 6, 8, 16 }; int n = 4; Console.Write(maxOR(arr, n)); } } // This code is contributed by 29AjayKumar |
24
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Maximum AND value of a pair in an array
- Maximum OR value of a pair in an array
- Maximum OR value of a pair in an Array without using OR operator
- Print pair with maximum AND value in an array
- Find a pair from the given array with maximum nCr value
- Find pair with maximum GCD in an array
- Maximum pair sum in the given index ranges of an Array
- Find the maximum sum pair in an Array with even parity
- 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 pair with maximum ratio in an Array
- Find a pair with maximum product in array of Integers
- Probability that a random pair chosen from an array (a[i], a[j]) has the maximum sum
- Probability of choosing a random pair with maximum sum in an array
- 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 possible GCD for a pair of integers with sum N
- Pair with maximum GCD from two arrays
- Maximum XOR value of a pair from a range
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.

