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++
// C++ program for the above approach #include <bits/stdc++.h> #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 bdef 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 athe 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 athe 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 Codeif __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 |
3
Time Complexity: O(N*M)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Sum of Bitwise AND of all pairs possible from two arrays
- Nth term of a recurrence relation generated by two given arrays
- Mean of array generated by products of all pairs of the given array
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- Maximize minimum of array generated by maximums of same indexed elements of two rows of a given Matrix
- Bitwise XOR of a submatrix of a matrix generated from a given array
- Maximize sum of pairwise products generated from the given Arrays
- Generate all possible sorted arrays from alternate elements of two given sorted arrays
- Minimum LCM and GCD possible among all possible sub-arrays
- Count rectangles generated in a given rectangle by lines drawn parallel to X and Y axis from a given set of points
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Count pairs with bitwise XOR exceeding bitwise AND from a given array
- Count sequences of given length having non-negative prefix sums that can be generated by given values
- String generated by typing given string in a keyboard having the button of given character faulty
- Find bitwise AND (&) of all possible sub-arrays
- Count pairs with equal Bitwise AND and Bitwise OR value
- Non-negative pairs with sum of Bitwise OR and Bitwise AND equal to N
- Find bitwise OR of all possible sub-arrays
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

