Maximum OR value of a pair in an array

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
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 = max(maxVal, arr[i] | arr[j]);
        }
  
    return maxVal;
}
  
// Driver code
int main()
{
    int arr[] = { 4, 8, 12, 16 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << maxOR(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
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 
    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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return the maximum bitwise OR
# for any pair of the given array
def 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 code
if __name__ == '__main__':
    arr = [4, 8, 12, 16];
    n = len(arr);
  
    print(maxOR(arr, n));
  
# This code is contributed by 29AjayKumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using 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.

chevron_right


Output:

28


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : AnkitRai01, jit_t, 29AjayKumar



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.