📌  相关文章
📜  从两个给定数组生成的对的所有按位与的最小可能按位或

📅  最后修改于: 2021-05-25 01:18:46             🧑  作者: Mango

给定两个阵列的常用3 []BRR [分别的长度NM],创建使用以下操作,使得在阵列RES所有元素的按位或[]是最小的长度为N的阵列RES []。

  • 对于arr []中的每个索引i,从数组brr []中选择任何索引j (允许重复)并更新res [i] = arr [i]和brr [j]

任务是打印数组元素res []的最小可能按位或的值。

例子:

天真的方法:最简单的方法是使用brr []生成数组arr []的所有可能对的按位或。完成上述步骤后,仅选择按位或为最小的N个元素。打印最小的按位或。

时间复杂度: O(2 (N * M) )
辅助空间: O(1)

高效的方法:为了优化上述方法,我们的想法是从最大可能的答案开始,然后在每个步骤中将其最小化。步骤如下:

  1. 初始化最大可能答案(例如ans ),该答案将是一个数字,其中所有位均已设置
  2. 遍历从310的每个位,并检查该位是否可以置位,因为将特定位设置为0将使总体答案最小。
  3. 对于上述步骤中的每个未设置位,请检查结果数组中的所有元素是否都将按位或作为当前可能的答案。如果发现是正确的,则以最小答案更新当前答案。
  4. 完成上述步骤后,打印存储在ans中的Bitwise最小可能值。

下面是上述方法的实现。

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& a,
        vector& 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& a,
                vector& 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 a = { 1, 2, 3 };
    vector 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 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 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


输出:
3

时间复杂度: O(N * M)
辅助空间: O(1)