Maximum OR of Two Numbers in an Array
Given an array Arr of non-negative integers of size N, the task is to find the maximum possible OR between two numbers present in the array.
Example:
Input: Arr = {25, 10, 2, 8, 5, 3}
Output: 29
Input: Arr = {1, 2, 3, 4, 5, 6, 7}
Output: 7
Naive Approach: A Simple Solution is to generate all pairs of the given array and compute OR of the pairs. Finally, return the maximum XOR value.
Time Complexity: O(N^2)
Auxiliary Space: O(1)
Better Approach: The approach is based on below Bitwise observation:
- If we set all bits of current arr[i], i.e. lets say arr[i]=10 (1010),
- and we change it to 15 (1111),
- and the current max OR calculated so far is greater than 15,
- then we dont need to check for arr[i], as it wont affect our ans.
This seems like a small change but it will reduce the time significantly.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxOR(vector< int >& nums)
{
int n = nums.size();
int ans = 0;
for ( int i = 0; i < n; i++) {
if (ans >= pow (
2, floor (log2(nums[i]) + 1))
- 1)
continue ;
for ( int j = 0; j < n; j++) {
if (i != j)
ans = max(ans,
nums[i] | nums[j]);
}
}
return ans;
}
int main()
{
vector< int > arr = { 1, 2, 3, 4, 5, 6, 7 };
cout << maxOR(arr) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static int maxOR(ArrayList<Integer> nums)
{
int n = nums.size();
int ans = 0 ;
for ( int i = 0 ; i < n; i++) {
if (ans >= Math.pow( 2 , Math.floor(
Math.log(nums.get(i)) + 1 )) - 1 )
continue ;
for ( int j = 0 ; j < n; j++) {
if (i != j)
ans = Math.max(ans, nums.get(i)
| nums.get(j));
}
}
return ans;
}
public static void main(String[] args)
{
ArrayList<Integer> arr = new ArrayList<>(
Arrays.asList( 1 , 2 , 3 , 4 , 5 , 6 , 7 ));
System.out.println(maxOR(arr));
}
}
|
Python3
from math import pow , floor, log2
def maxOR(nums):
n = len (nums)
ans = 0
for i in range (n):
if (ans > = pow ( 2 , floor(log2(nums[i]) + 1 )) - 1 ):
continue
for j in range (n):
if (i ! = j):
ans = max (ans, nums[i] | nums[j])
return ans
arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]
print (maxOR(arr))
|
C#
using System;
class GFG
{
public static int maxOR( int [] nums)
{
int n = nums.Length;
int ans = 0;
for ( int i = 0; i < n; i++)
{
if (ans >= Math.Pow(2, Math.Floor(
Math.Log(nums[i]) + 1)) - 1)
continue ;
for ( int j = 0; j < n; j++)
{
if (i != j)
ans = Math.Max(ans, nums[i]
| nums[j]);
}
}
return ans;
}
public static void Main()
{
int [] arr = { 1, 2, 3, 4, 5, 6, 7 };
Console.Write(maxOR(arr));
}
}
|
Javascript
<script>
function maxOR(nums) {
let n = nums.length;
let ans = 0;
for (let i = 0; i < n; i++) {
if (ans >= Math.pow(
2, Math.floor(Math.log2(nums[i]) + 1))
- 1)
continue ;
for (let j = 0; j < n; j++) {
if (i != j)
ans = Math.max(ans,
nums[i] | nums[j]);
}
}
return ans;
}
let arr = [1, 2, 3, 4, 5, 6, 7];
document.write(maxOR(arr) + '<br>' );
</script>
|
Time Complexity: O(N^2)
Auxiliary Space: O(1)
Last Updated :
09 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...