Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
Last Updated :
29 Nov, 2022
Given two arrays arr[] and brr[] of length N and M respectively, create an array res[] of length N using the below operations such that the Bitwise OR of all the elements in the array res[] is minimum.
- For every index i in arr[], choose any index j(repetitions allowed) from the array brr[] and update res[i] = arr[i] & brr[j]
The task is to print the value of the minimum possible Bitwise OR of the array elements res[].
Examples:
Input: arr[] = {2, 1}, brr[] = {4, 6, 7}
Output: 0
Explanation:
For the array arr[] = {2, 1}, choose the values from brr[] as {4, 4} or {4, 6}.
Therefore, the resultant array becomes {0, 0} and the bitwise OR of the resultant array = 0 which is the minimum possible value.
Input: arr[] = {1, 2, 3}, brr[] = {7, 15}
Output: 3
Explanation:
For the array arr[] = {1, 2, 3}, choose the values from brr[] as {7, 7, 7}.
Therefore, the resultant array becomes {1, 2, 3} and the bitwise OR of the resultant array = 3.
Naive Approach: The simplest approach is to generate Bitwise OR of all possible pairs of the arrays arr[] with brr[]. After completing the above steps, choose only those N elements whose Bitwise OR is minimum. Print that minimum Bitwise OR.
Time Complexity: O(2(N * M))
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to start from the maximum possible answer and then try to minimize it in every step. Below are the steps:
- Initialize the maximum possible answer(say ans) which will be a number in which all the bits are set.
- Traverse through every bit from 31 to 0 and check if that bit can be unset or not as making a particular bit as 0 will minimize the overall answer.
- For each unset bit in the above steps, check if all the elements of the resultant array will give the bitwise OR as the current possible answer or not. If found to be true, then update the current answer with the minimum answer.
- After completing the above steps, print the value of the minimum possible value of Bitwise stored in ans.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
#define ll long long
using namespace std;
bool check(ll no, vector< int >& a,
vector< int >& b)
{
int count = 0;
int n = a.size();
int m = b.size();
for ( int i = 0; i < n; ++i) {
for ( int j = 0; j < m; ++j) {
if (((a[i] & b[j]) | no) == no) {
count++;
break ;
}
}
}
if (count == n)
return true ;
else
return false ;
}
ll findMinValue(vector< int >& a,
vector< int >& b)
{
ll ans = (1LL << 31) - 1;
for (ll i = 30; i >= 0; --i) {
if (check(ans ^ (1 << i), a, b)) {
ans = ans ^ (1 << i);
}
}
return ans;
}
int main()
{
vector< int > a = { 1, 2, 3 };
vector< int > b = { 7, 15 };
cout << findMinValue(a, b);
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean check( int no,
int []a, int []b)
{
int count = 0 ;
int n = a.length;
int m = b.length;
for ( int i = 0 ; i < n; ++i)
{
for ( int j = 0 ; j < m; ++j)
{
if (((a[i] & b[j]) | no) == no)
{
count++;
break ;
}
}
}
if (count == n)
return true ;
else
return false ;
}
static int findMinValue( int []a,
int []b)
{
int ans = ( 1 << 31 ) - 1 ;
for ( int i = 30 ; i >= 0 ; --i)
{
if (check(ans ^ ( 1 << i), a, b))
{
ans = ans ^ ( 1 << i);
}
}
return ans;
}
public static void main(String[] args)
{
int []a = { 1 , 2 , 3 };
int []b = { 7 , 15 };
System.out.print(findMinValue(a, b));
}
}
|
Python3
def check(no, a, b):
count = 0
n = len (a)
m = len (b)
for i in range (n):
for j in range (m):
if (((a[i] & b[j]) | no) = = no):
count + = 1
break
if (count = = n):
return True
else :
return False
def findMinValue(a, b):
ans = ( 1 << 31 ) - 1
for i in range ( 30 , - 1 , - 1 ):
if (check(ans ^ ( 1 << i), a, b)):
ans = ans ^ ( 1 << i)
return ans
if __name__ = = '__main__' :
a = [ 1 , 2 , 3 ]
b = [ 7 , 15 ]
print (findMinValue(a, b))
|
C#
using System;
class GFG{
static bool check( int no, int []a,
int []b)
{
int count = 0;
int n = a.Length;
int m = b.Length;
for ( int i = 0; i < n; ++i)
{
for ( int j = 0; j < m; ++j)
{
if (((a[i] & b[j]) | no) == no)
{
count++;
break ;
}
}
}
if (count == n)
return true ;
else
return false ;
}
static int findMinValue( int []a,
int []b)
{
int ans = int .MaxValue;
for ( int i = 30; i >= 0; --i)
{
if (check(ans ^ (1 << i), a, b))
{
ans = ans ^ (1 << i);
}
}
return ans;
}
public static void Main(String[] args)
{
int []a = { 1, 2, 3 };
int []b = { 7, 15 };
Console.Write(findMinValue(a, b));
}
}
|
Javascript
<script>
function check(no,
a, b)
{
let count = 0;
let n = a.length;
let m = b.length;
for (let i = 0; i < n; ++i)
{
for (let j = 0; j < m; ++j)
{
if (((a[i] & b[j]) | no) == no)
{
count++;
break ;
}
}
}
if (count == n)
return true ;
else
return false ;
}
function findMinValue(a,b)
{
let ans = (1 << 31) - 1;
for (let i = 30; i >= 0; --i)
{
if (check(ans ^ (1 << i), a, b))
{
ans = ans ^ (1 << i);
}
}
return ans;
}
let a = [1, 2, 3];
let b = [7, 15];
document.write(findMinValue(a, b));
</script>
|
Time Complexity: O(N*M)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...