Maximum OR value of a pair in an array Last Updated : 11 Apr, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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++ // 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; } Java // 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 Python3 # 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 C# // 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. JavaScript <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> Output: 28 Time Complexity: O(n*n)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Maximum even sum of a pair of given Array A Akshita207 Follow Improve Article Tags : DSA Arrays Bitwise-OR Practice Tags : Arrays Similar Reads Maximum OR value of a pair in an Array | Set 2 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 5 min read Maximum AND value of a pair in an array Given an array of N positive elements, the task is to find the maximum AND value generated by any pair of elements from the array. Examples: Input: arr1[] = {16, 12, 8, 4}Output: 8Explanation: 8 AND12 will give us the maximum value 8 Input: arr1[] = {4, 8, 16, 2}Output: 0 Recommended PracticeMaximum 12 min read Print pair with maximum AND value in an array Given an array of n positive elements, find the maximum AND value and the pair of elements generating the maximum AND value from the array. AND is bitwise & operator. Examples: Input : arr[] = {4, 8, 12, 16} Output : Pair = 8, 12 Maximum AND value = 8 Input : arr[] = {4, 8, 16, 2} Output : Pair 9 min read Maximum value of arr[i] + arr[j] + i â j for any pair of an array Given an array arr[] consisting of N integers, the task is to find the maximum value of (arr[i] + arr[j] + i ? j) for any possible pair (i, j) of the given array. Examples: Input: arr[] = {1, 9, 3, 6, 5}Output: 13Explanation:The pair of the array having the maximum value of (arr[i] + arr[j] + i ? j) 9 min read Maximum even sum of a pair of given Array Given an array arr[] of N distinct non-negative integers, the task is to determine if there is an even number that can be represented as the sum of two different elements of arr. If it exists return the maximum such number otherwise return -1. Examples: Input: N = 4, arr[] = {2, 3, 4, 5}Output: 8Exp 12 min read Maximum sum of minimums of pairs in an array Given an array arr[] of N integers where N is even, the task is to group the array elements in the pairs (X1, Y1), (X2, Y2), (X3, Y3), ... such that the sum min(X1, Y1) + min(X2, Y2) + min(X3, Y3) + ... is maximum.Examples: Input: arr[] = {1, 5, 3, 2} Output: 4 (1, 5) and (3, 2) -> 1 + 2 = 3 (1, 4 min read Like