📜  按位或等于n的最大集合

📅  最后修改于: 2021-05-25 04:58:41             🧑  作者: Mango

给定一个整数n,找到按位或等于n的最大可能的非负整数集。

例子:

Input : n = 5
Output : arr[] = [0, 1, 4, 5]
The bitwise OR of 0, 1, 4 and 5 equals 5. 
It is not possible to obtain a set larger than this.

Input : n = 8
Output : arr[] = [0, 8]

先决条件:按位或等于k的最大子集

上面引用的文章与这篇文章的不同之处在于要检查的元素数量。在上面引用的文章中,我们有n个数字组成的数组,在本文中,我们有整个非负数的集合。

使用O(N)的时间复杂度来遍历数组很简单,但是遍历无穷大的非负数集是不可能的。那么,我们如何将自己限制在较小的一组数字上呢?

答案在于所使用的概念。对于x大于n的任何数字,x和n的按位“或”将永远不等于n。
因此,我们只需要从0遍历到n即可得到答案。

第二个区别是,这个问题总是有答案的。另一方面,上述文章中是否存在答案尚不确定。这是因为我们总是可以将n包含在结果集中。

算法:
遍历从0到n的数字,并与n进行按位或运算。如果按位或等于n,则在结果集中包括该数字。

C++
// CPP Program to find the largest set
// with bitwise OR equal to n
#include 
using namespace std;
  
// function to find the largest set with
// bitwise OR equal to n
void setBitwiseORk(int n)
{
    vector v;
  
    for (int i = 0; i <= n; i++) {
  
        // If the bitwise OR of n and i
        // is equal to n, then include i
        // in the set
        if ((i | n) == n)
            v.push_back(i);
    }
  
    for (int i = 0; i < v.size(); i++)
        cout << v[i] << ' ';
}
  
// Driver Code
int main()
{
    int n = 5;
  
    setBitwiseORk(n);
    return 0;
}


Java
// Java Program to find the largest set
// with bitwise OR equal to n
import java.util.*;
  
class GFG 
{
  
    // function to find the largest set with
    // bitwise OR equal to n
    static void setBitwiseORk(int n) 
    {
        Vector v = new Vector();
  
        for (int i = 0; i <= n; i++)
        {
  
            // If the bitwise OR of n and i
            // is equal to n, then include i
            // in the set
            if ((i | n) == n) 
            {
                v.add(i);
            }
        }
  
        for (int i = 0; i < v.size(); i++)
        {
            System.out.print(v.get(i) + " ");
        }
    }
  
    // Driver Code
    public static void main(String[] args) 
    {
        int n = 5;
  
        setBitwiseORk(n);
    }
}
  
/* This code contributed by PrinciRaj1992 */


Python3
# Python 3 Program to find the largest 
# set with bitwise OR equal to n
  
# function to find the largest set 
# with bitwise OR equal to n
def setBitwiseORk(n):
    v = []
  
    for i in range(0, n + 1, 1):
          
        # If the bitwise OR of n and i
        # is equal to n, then include i
        # in the set
        if ((i | n) == n):
            v.append(i)
  
    for i in range(0, len(v), 1):
        print(v[i], end = ' ')
  
# Driver Code
if __name__ == '__main__':
    n = 5
  
    setBitwiseORk(n)
  
# This code is contributed by 
# Surendra_Gangwar


C#
// C# Program to find the largest set
// with bitwise OR equal to n
using System; 
using System.Collections.Generic;
      
class GFG 
{
  
    // function to find the largest set with
    // bitwise OR equal to n
    static void setBitwiseORk(int n) 
    {
        List v = new List();
  
        for (int i = 0; i <= n; i++)
        {
  
            // If the bitwise OR of n and i
            // is equal to n, then include i
            // in the set
            if ((i | n) == n) 
            {
                v.Add(i);
            }
        }
  
        for (int i = 0; i < v.Count; i++)
        {
            Console.Write(v[i] + " ");
        }
    }
  
    // Driver Code
    public static void Main(String[] args) 
    {
        int n = 5;
  
        setBitwiseORk(n);
    }
}
  
// This code has been contributed by 29AjayKumar


输出:

0 1 4 5

时间复杂度: O(N)