Maximum OR value of a pair in an Array | Set 2
Last Updated :
20 Apr, 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[] = {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.
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
Python3
# Python3 implementation of
# the approach above
# Function to return the maximum
# bitwise OR for any pair of the
# given array in O(n) time complexity.
def maxOR(arr, n):
# Find the maximum
# element in
max_value = max(arr)
# Stores the maximum
# OR value the array
ans = 0
# Traverse the array and
# perform Bitwise OR
# between every array element
# with the maximum element
for i in range(n):
ans = max(ans, (max_value | arr[i]))
return ans
# Driver Code
if __name__ == "__main__":
arr = [ 3, 6, 8, 16 ]
n = len(arr)
print(maxOR(arr, n))
# This code is contributed by jana_sayantan
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
JavaScript
<script>
// Javascript implementation of
// the above approach
// Function to return the maximum
// bitwise OR for any pair of the
// given array in O(n) time complexity.
function maxOR(arr, n)
{
// Find the maximum
// element in the array
let max_value = Math.max(...arr);
// Stores the maximum
// OR value
let ans = 0;
// Traverse the array and
// perform Bitwise OR
// between every array element
// with the maximum element
for (let i = 0; i < n; i++)
{
ans = Math.max(ans, (max_value | arr[i]));
}
return ans;
}
// Driver Code
let arr = [ 3, 6, 8, 16 ];
let n = 4;
document.write(maxOR(arr, n));
// This code is contributed by souravghosh0416.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
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 = 24Input: ar
6 min read
Number of pairs whose sum is a power of 2 Given an array arr[] of positive integers, the task is to count the maximum possible number of pairs (arr[i], arr[j]) such that arr[i] + arr[j] is a power of 2. Note: One element can be used at most once to form a pair. Examples: Input: arr[] = {3, 11, 14, 5, 13} Output: 2 All valid pairs are (13,
9 min read
C++ Program for Number of pairs with maximum sum Given an array arr[], count number of pairs arr[i], arr[j] such that arr[i] + arr[j] is maximum and i Example : Input : arr[] = {1, 1, 1, 2, 2, 2} Output : 3 Explanation: The maximum possible pair sum where i Method 1 (Naive)Â Traverse a loop i from 0 to n, i.e length of the array and another loop j
3 min read
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 ove
4 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