📜  按位或等于k的最大子集

📅  最后修改于: 2021-04-26 18:54:42             🧑  作者: Mango

给定一个非负整数和整数k的数组,请找到按位或等于k的最大长度子集。

例子 :

Input : arr[] = [1, 4, 2]
        k = 3
Output : [1, 2]
Explanation: The bitwise OR of 
1 and 2 equals 3. It is not possible to obtain 
a subset of length greater than 2.

Input : arr[] = [1, 2, 5]
        k = 4
Output : []
No subset's bitwise OR equals 4.

方法1(简单):
天真的方法是考虑所有子集。在考虑子集时,计算其按位或。如果等于k,则将子集的长度与到目前为止的最大长度进行比较,并在需要时更新最大长度。

方法2(有效):
0或0 = 0
1或0 = 1
1或1 = 1
因此,对于k的二进制表示中所有具有等于0的位的位置,所得子集中所有元素的二进制表示中的对应位置都必须为0。
另一方面,对于k中的位等于1的位置,在对应位置必须至少有一个元素为1的元素。其余元素在该位置可以为0或1,这无关紧要。

因此,要获得结果子集,请遍历初始数组。在确定元素是否应位于结果子集中时,请检查k的二进制表示形式中是否有任何位置为0,并且该元素中的对应位置为1。如果存在这样的位置,则忽略该元素,否则将其包含在结果子集中。

如何确定k的二进制表示中是否存在一个位置为0且元素中的对应位置为1的位置?
只需对k与该元素进行按位或运算。如果它不等于k,则存在这样的位置,因此必须忽略该元素。如果它们的按位或等于k,则将当前元素包括在结果子集中。

最后一步是确定是否存在至少一个元素,该元素在k中的对应位置中的位置为1,而位置1中的位置为1。
只需计算所得子集的按位或。如果等于k,则这是最终答案。否则不存在满足条件的子集。

C++
// CPP Program to find the maximum subset
// with bitwise OR equal to k
#include 
using namespace std;
  
// function to find the maximum subset with
// bitwise OR equal to k
void subsetBitwiseORk(int arr[], int n, int k)
{
    vector v;
  
    for (int i = 0; i < n; i++) {
  
        // If the bitwise OR of k and element
        // is equal to k, then include that element
        // in the subset
        if ((arr[i] | k) == k)
            v.push_back(arr[i]);
    }
  
    // Store the bitwise OR of elements in v
    int ans = 0;
  
    for (int i = 0; i < v.size(); i++)
        ans |= v[i];
  
    // If ans is not equal to k, subset doesn't exist
    if (ans != k) {
        cout << "Subset does not exist" << endl;
        return;
    }
  
    for (int i = 0; i < v.size(); i++)
        cout << v[i] << ' ';
}
  
// Driver Code
int main()
{
    int k = 3;
    int arr[] = { 1, 4, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    subsetBitwiseORk(arr, n, k);
    return 0;
}


Java
// Java Program to find the maximum subset
// with bitwise OR equal to k
import java.util.*;
  
class GFG {
  
    // function to find the maximum subset 
    // with bitwise OR equal to k
    static void subsetBitwiseORk(int arr[],
                              int n, int k)
    {
        ArrayList v = 
                  new ArrayList();
      
        for (int i = 0; i < n; i++) {
      
            // If the bitwise OR of k and 
            // element is equal to k, then
            // include that element in the 
            // subset
            if ((arr[i] | k) == k){
                v.add(arr[i]);
            }
        }
      
        // Store the bitwise OR of elements
        // in v
        int ans = 0;
      
        for (int i = 0; i < v.size(); i++)
            ans = ans|v.get(i);
      
        // If ans is not equal to k, subset
        // doesn't exist
        if (ans != k) {
            System.out.println("Subset does"
                           + " not exist" );
            return;
        }
      
        for (int i = 0; i < v.size(); i++)
            System.out.print(v.get(i) + " " );
    }
      
    // main function
    public static void main(String[] args)
    {
        int k = 3;
        int arr[] = { 1, 4, 2 };
        int n = arr.length;
      
        subsetBitwiseORk(arr, n, k);
          
    }
}
  
// This code is contributed by Arnab Kundu.


Python3
# Python3 Program to find the 
# maximum subset with bitwise
# OR equal to k
    
# function to find the maximum 
# subset with bitwise OR equal to k
def subsetBitwiseORk(arr, n, k) :
    v = []
    
    for i in range(0, n) : 
        # If the bitwise OR of k 
        # and element is equal to k,
        # then include that element
        # in the subset
        if ((arr[i] | k) == k) :
            v.append(arr[i])
    
    # Store the bitwise OR
    # of elements in v
    ans = 0
    
    for i in range(0, len(v)) :
        ans |= v[i]
    
    # If ans is not equal to
    # k, subset doesn't exist
    if (ans != k) :
        print ("Subset does not exist\n")
        return
    
    for i in range(0, len(v)) :
        print ("{} ".format(v[i]), end="")
    
# Driver Code
k = 3
arr = [1, 4, 2]
n = len(arr)
    
subsetBitwiseORk(arr, n, k)
    
# This code is contributed by 
# Manish Shaw(manishshaw1)


C#
// C# Program to find the maximum subset
// with bitwise OR equal to k
using System;
using System.Collections;
  
class GFG {
  
    // function to find the maximum subset
    // with bitwise OR equal to k
    static void subsetBitwiseORk(int []arr, 
                              int n, int k)
    {
        ArrayList v = new ArrayList();
      
        for (int i = 0; i < n; i++) {
      
            // If the bitwise OR of k and 
            // element is equal to k, then
            // include that element in the
            // subset
            if ((arr[i] | k) == k){
                v.Add(arr[i]);
            }
        }
      
        // Store the bitwise OR of 
        // elements in v
        int ans = 0;
      
        for (int i = 0; i < v.Count; i++)
            ans = ans|(int)v[i];
      
        // If ans is not equal to k, subset
        // doesn't exist
        if (ans != k) {
            Console.WriteLine("Subset does"
                          + " not exist" );
            return;
        }
      
        for (int i = 0; i < v.Count; i++)
            Console.Write((int)v[i] + " " );
    }
      
    // main function
    static public void Main(String []args)
    {
        int k = 3;
        int []arr = { 1, 4, 2 };
        int n = arr.Length;
      
        subsetBitwiseORk(arr, n, k);
          
    }
}
  
// This code is contributed by Arnab Kundu


PHP


输出 :

1 2

时间复杂度: O(N),其中N是数组的大小。