Maximum OR value of a pair in an array - GeeksforGeeks

20 May, 2021
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[] = {4, 8, 12, 16} Output: 28 (12, 16) is the pair with the maximum bitwise OR. 12 | 16 = 28
Input: arr[] = {4, 8, 16, 2} Output: 24 
Approach: Iterate over all the possible pairs and calculate the OR value of these pairs. Finally, print the maximum of all the values.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// 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 arrayint maxOR(int arr[], int n){     // To store the maximum OR value    int maxVal = 0;     // For every possible pair    for (int i = 0; i < n - 1; i++)        for (int j = i + 1; j < n; j++) {             // Update the maximum OR value            maxVal = max(maxVal, arr[i] | arr[j]);        }     return maxVal;} // Driver codeint main(){    int arr[] = { 4, 8, 12, 16 };    int n = sizeof(arr) / sizeof(arr[0]);     cout << maxOR(arr, n);     return 0;}
// Java implementation of the approachclass GFG {     // Function to return the maximum bitwise OR    // for any pair of the given array    static int maxOR(int arr[], int n)    {         // To store the maximum OR value        int maxVal = 0;         // For every possible pair        for (int i = 0; i < n - 1; i++)            for (int j = i + 1; j < n; j++) {                 // Update the maximum OR value                maxVal = Math.max(maxVal, arr[i] | arr[j]);            }         return maxVal;    }     // Driver code    public static void main(String[] args)    {        int arr[] = { 4, 8, 12, 16 };        int n = arr.length;         System.out.println(maxOR(arr, n));    }} // This code is contributed by AnkitRai01
# Python3 implementation of the approach # Function to return the maximum bitwise OR# for any pair of the given arraydef maxOR(arr, n):         # To store the maximum OR value    maxVal = 0;     # For every possible pair    for i in range(n - 1):        for j in range(i + 1, n):                         # Update the maximum OR value            maxVal = max(maxVal, arr[i] | arr[j]);     return maxVal; # Driver codeif __name__ == '__main__':    arr = [4, 8, 12, 16];    n = len(arr);     print(maxOR(arr, n)); # This code is contributed by 29AjayKumar
// C# implementation of the approachusing System; class GFG {     // Function to return the maximum bitwise OR    // for any pair of the given array    static int maxOR(int[] arr, int n)    {         // To store the maximum OR value        int maxVal = 0;         // For every possible pair        for (int i = 0; i < n - 1; i++)            for (int j = i + 1; j < n; j++) {                 // Update the maximum OR value                maxVal = Math.Max(maxVal, arr[i] | arr[j]);            }         return maxVal;    }     // Driver code    static public void Main()    {        int[] arr = { 4, 8, 12, 16 };        int n = arr.Length;         Console.Write(maxOR(arr, n));    }} // This code is contributed by ajit.
<script>    // Javascript implementation of the approach           // Function to return the maximum bitwise OR    // for any pair of the given array    function maxOR(arr, n)    {          // To store the maximum OR value        let maxVal = 0;          // For every possible pair        for (let i = 0; i < n - 1; i++)            for (let j = i + 1; j < n; j++) {                  // Update the maximum OR value                maxVal = Math.max(maxVal, arr[i] | arr[j]);            }          return maxVal;    }         let arr = [ 4, 8, 12, 16 ];    let n = arr.length;     document.write(maxOR(arr, n));</script>
28
 
Efficient Approach: As we have to find such a pair that will give maximum OR value i.e, our output must have set bits such that it will form the maximum OR value pair. For this we will find maximum element in our array as it has set bits arrangement contributing in maximum value and hence we only have to find maximum OR value with maximum array element, as there must be a pair giving max OR value where one element is the maximum array element.
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach ( Linear time Complexity ) #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 maximum element in the array    int max_value = *max_element(arr, arr + n);     // To store the maximum OR value    int ans = 0;     // iterate over rest array elements and find maximum OR value pair    for (int i = 0; i < n; i++) {        ans = max(ans, (max_value | arr[i]));    }    return ans;} // Driver codeint main(){    int arr[] = { 3, 6, 8, 16 };    int n = sizeof(arr) / sizeof(arr[0]);     cout << maxOR(arr, n);     return 0;}
// Java implementation of the approach// (Linear time Complexity)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.public static int maxOR(int[] arr, int n){         // Find maximum element in the array    int max_value = Arrays.stream(arr).max().getAsInt();         // To store the maximum OR value    int ans = 0;       // Iterate over rest array elements and    // find maximum OR value pair    for(int i = 0; i < n; i++)    {        ans = Math.max(ans, (max_value | arr[i]));    }    return ans;} // Driver codepublic static void main(String[] args){    int arr[] = { 3, 6, 8, 16 };    int n = arr.length;         System.out.print(maxOR(arr, n));}} // This code is contributed by divyeshrabadiya07
# Python3 implementation of the approach# (Linear time Complexity)  # Function to return the maximum bitwise# OR for any pair of the given arraydef maxOR(arr, n):          # To store the maximum element    # of the array    maxVal = max(arr)         # To store the maximum OR value    ans = 0         # Iterate over rest array elements and    # find maximum OR value pair    for i in range(n - 1):                 # Update the maximum OR value        ans = max(ans, maxVal | arr[i])       return ans   # Driver codeif __name__ == '__main__':         arr = [ 3, 6, 8, 16 ]    n = len(arr)         print(maxOR(arr, n))     # This code is contributed by math_lover
// C# implementation of the approach// (Linear time Complexity)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 maximum element in the array    int max_value = arr.Max();     // To store the maximum OR value    int ans = 0;     // Iterate over rest array elements and    // find maximum OR value pair    for(int i = 0; i < n; i++)    {        ans = Math.Max(ans, (max_value | arr[i]));    }    return ans;} // Driver codepublic static void Main(String[] args){    int []arr = { 3, 6, 8, 16 };    int n = arr.Length;     Console.Write(maxOR(arr, n));}} // This code is contributed by doreamon_
<script>    // Javascript implementation of the approach    // (Linear time Complexity)         // Function to return the maximum bitwise    // OR for any pair of the given array    // in O(n) time complexity.    function maxOR(arr, n)    {         // Find maximum element in the array        let max_value = Number.MIN_VALUE;        for(let i = 0; i < n; i++)        {            max_value = Math.max(max_value, arr[i]);        }         // To store the maximum OR value        let ans = 0;         // Iterate over rest array elements and        // find maximum OR value pair        for(let i = 0; i < n; i++)        {            ans = Math.max(ans, (max_value | arr[i]));        }        return ans;    }         let arr = [ 3, 6, 8, 16 ];    let n = arr.length;      document.write(maxOR(arr, n));     </script>
Output:
24
Time Complexity: O(n) 
ankthon
jit_t
29AjayKumar
yomad_
divyeshrabadiya07
math_lover
doreamon_
suresh07
divyesh072019
Bitwise-OR
Arrays
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Stack Data Structure (Introduction and Program)
Introduction to Arrays
Multidimensional Arrays in Java
Linear Search
Linked List vs Array
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Python | Using 2D arrays/lists the right way
Search an element in a sorted and rotated array