 Open in App
Not now

# 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:
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:
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:

1. Initialize the maximum possible answer(say ans) which will be a number in which all the bits are set.
2. 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.
3. 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.
4. 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++

 `// C++ program for the above approach` `#include ``#define ll long long``using` `namespace` `std;` `// Function to check if it is possible``// to make current no as minimum result``// using array a and b``bool` `check(ll no, vector<``int``>& a,``        ``vector<``int``>& b)``{``    ``int` `count = 0;` `    ``// Size of the first array``    ``int` `n = a.size();` `    ``// Size of the second array``    ``int` `m = b.size();` `    ``for` `(``int` `i = 0; i < n; ++i) {` `        ``for` `(``int` `j = 0; j < m; ++j) {` `            ``// Check for the number as``            ``// ans is possible or not``            ``if` `(((a[i] & b[j]) | no) == no) {``                ``count++;``                ``break``;``            ``}``        ``}``    ``}` `    ``// If all the elements of array``    ``// gives no as the Bitwise OR``    ``if` `(count == n)``        ``return` `true``;``    ``else``        ``return` `false``;``}` `// Function to find the minimum bitwise``// OR of all the element in res[]``ll findMinValue(vector<``int``>& a,``                ``vector<``int``>& b)``{``    ``// Max possible ans``    ``ll ans = (1LL << 31) - 1;` `    ``for` `(ll i = 30; i >= 0; --i) {` `        ``// Check for the upper bit that``        ``// can be make off or not``        ``if` `(check(ans ^ (1 << i), a, b)) {``            ``ans = ans ^ (1 << i);``        ``}``    ``}` `    ``return` `ans;``}` `// Driver Code``int` `main()``{``    ``// Given array a[] and b[]``    ``vector<``int``> a = { 1, 2, 3 };``    ``vector<``int``> b = { 7, 15 };` `    ``// Function Call``    ``cout << findMinValue(a, b);` `    ``return` `0;``}`

## Java

 `// Java program for``// the above approach``import` `java.util.*;``class` `GFG{` `// Function to check if it is possible``// to make current no as minimum result``// using array a and b``static` `boolean` `check(``int` `no,``                     ``int` `[]a, ``int` `[]b)``{``  ``int` `count = ``0``;` `  ``// Size of the first array``  ``int` `n = a.length;` `  ``// Size of the second array``  ``int` `m = b.length;` `  ``for` `(``int` `i = ``0``; i < n; ++i)``  ``{``    ``for` `(``int` `j = ``0``; j < m; ++j)``    ``{``      ``// Check for the number as``      ``// ans is possible or not``      ``if` `(((a[i] & b[j]) | no) == no)``      ``{``        ``count++;``        ``break``;``      ``}``    ``}``  ``}` `  ``// If all the elements of array``  ``// gives no as the Bitwise OR``  ``if` `(count == n)``    ``return` `true``;``  ``else``    ``return` `false``;``}` `// Function to find the minimum``// bitwise OR of all the``// element in res[]``static` `int` `findMinValue(``int` `[]a,``                        ``int` `[]b)``{``  ``// Max possible ans``  ``int` `ans = (``1` `<< ``31``) - ``1``;` `  ``for` `(``int` `i = ``30``; i >= ``0``; --i)``  ``{``    ``// Check for the upper bit that``    ``// can be make off or not``    ``if` `(check(ans ^ (``1` `<< i), a, b))``    ``{``      ``ans = ans ^ (``1` `<< i);``    ``}``  ``}` `  ``return` `ans;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``  ``// Given array a[] and b[]``  ``int` `[]a = {``1``, ``2``, ``3``};``  ``int` `[]b = {``7``, ``15``};` `  ``// Function Call``  ``System.out.print(findMinValue(a, b));``}``}` `// This code is contributed by shikhasingrajput`

## Python3

 `# Python3 program for the above approach` `# Function to check if it is possible``# to make current no as minimum result``# using array a and b``def` `check(no, a, b):``    ` `    ``count ``=` `0` `    ``# Size of the first array``    ``n ``=` `len``(a)` `    ``# Size of the second array``    ``m ``=` `len``(b)` `    ``for` `i ``in` `range``(n):``        ``for` `j ``in` `range``(m):` `            ``# Check for the number as``            ``# ans is possible or not``            ``if` `(((a[i] & b[j]) | no) ``=``=` `no):``                ``count ``+``=` `1``                ``break` `    ``# If all the elements of array``    ``# gives no as the Bitwise OR``    ``if` `(count ``=``=` `n):``        ``return` `True``    ``else``:``        ``return` `False` `# Function to find the minimum bitwise``# OR of all the element in res[]``def` `findMinValue(a, b):``    ` `    ``# Max possible ans``    ``ans ``=` `(``1` `<< ``31``) ``-` `1` `    ``for` `i ``in` `range``(``30``, ``-``1``, ``-``1``):` `        ``# Check for the upper bit that``        ``# can be make off or not``        ``if` `(check(ans ^ (``1` `<< i), a, b)):``            ``ans ``=` `ans ^ (``1` `<< i)` `    ``return` `ans` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``# Given array a[] and b[]``    ``a ``=` `[ ``1``, ``2``, ``3` `]``    ``b ``=` `[ ``7``, ``15` `]` `    ``# Function call``    ``print``(findMinValue(a, b))` `# This code is contributed by mohit kumar 29`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{` `// Function to check if it is possible``// to make current no as minimum result``// using array a and b``static` `bool` `check(``int` `no, ``int` `[]a,``                          ``int` `[]b)``{``    ``int` `count = 0;``    ` `    ``// Size of the first array``    ``int` `n = a.Length;``    ` `    ``// Size of the second array``    ``int` `m = b.Length;``    ` `    ``for``(``int` `i = 0; i < n; ++i)``    ``{``        ``for``(``int` `j = 0; j < m; ++j)``        ``{``            ` `            ``// Check for the number as``            ``// ans is possible or not``            ``if` `(((a[i] & b[j]) | no) == no)``            ``{``                ``count++;``                ``break``;``            ``}``        ``}``    ``}``    ` `    ``// If all the elements of array``    ``// gives no as the Bitwise OR``    ``if` `(count == n)``        ``return` `true``;``    ``else``        ``return` `false``;``}` `// Function to find the minimum``// bitwise OR of all the``// element in res[]``static` `int` `findMinValue(``int` `[]a,``                        ``int` `[]b)``{``    ` `    ``// Max possible ans``    ``int` `ans = ``int``.MaxValue;``    ` `    ``for``(``int` `i = 30; i >= 0; --i)``    ``{``        ` `        ``// Check for the upper bit that``        ``// can be make off or not``        ``if` `(check(ans ^ (1 << i), a, b))``        ``{``            ``ans = ans ^ (1 << i);``        ``}``    ``}``    ``return` `ans;``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ` `    ``// Given array []a and []b``    ``int` `[]a = { 1, 2, 3 };``    ``int` `[]b = { 7, 15 };``    ` `    ``// Function call``    ``Console.Write(findMinValue(a, b));``}``}` `// This code is contributed by Amit Katiyar`

## Javascript

 ``

Output:

`3`

Time Complexity: O(N*M)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up