📜  用正位与求最大子集的大小

📅  最后修改于: 2022-05-13 01:56:09.025000             🧑  作者: Mango

用正位与求最大子集的大小

给定一个由N个正整数组成的数组arr[] ,任务是找到数组arr[]的子集的最大大小,其中包含正的按位与。

例子:

方法:给定的问题可以通过计算所有数组元素的每个对应位位置的设置位的数量来解决,然后任何位置的最大设置位的计数是所需的子集的最大计数,因为所有的位与这些元素总是积极的。请按照以下步骤解决给定的问题:

  • 初始化一个数组,比如大小为32bit[] ,它存储每个第 i 位位置的设置位计数。
  • 遍历给定的数组,对于每个元素,如果在arr[i]中设置了第 i 位,则说arr[i]增加数组bit[]第 i 位的频率。
  • 经过以上步骤,打印数组bit[]的最大值,打印子集的最大大小。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the largest possible
// subset having Bitwise AND positive
void largestSubset(int a[], int N)
{
    // Stores the number of set bits
    // at each bit position
    int bit[32] = { 0 };
 
    // Traverse the given array arr[]
    for (int i = 0; i < N; i++) {
 
        // Current bit position
        int x = 31;
 
        // Loop till array element
        // becomes zero
        while (a[i] > 0) {
 
            // If the last bit is set
            if (a[i] & 1 == 1) {
 
                // Increment frequency
                bit[x]++;
            }
 
            // Divide array element by 2
            a[i] = a[i] >> 1;
 
            // Decrease the bit position
            x--;
        }
    }
 
    // Size of the largest possible subset
    cout << *max_element(bit, bit + 32);
}
 
// Driver Code
int main()
{
    int arr[] = { 7, 13, 8, 2, 3 };
    int N = sizeof(arr) / sizeof(arr[0]);
    largestSubset(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG
{
   
      static void largestSubset(int a[], int N)
    {
 
        // Stores the number of set bits
        // at each bit position
        int bit[] = new int[32];
 
        // Traverse the given array arr[]
        for (int i = 0; i < N; i++) {
 
            // Current bit position
            int x = 31;
 
            // Loop till array element
            // becomes zero
            while (a[i] > 0) {
 
                // If the last bit is set
                if ((int)(a[i] & 1) == (int)1) {
 
                    // Increment frequency
                    bit[x]++;
                }
 
                // Divide array element by 2
                a[i] = a[i] >> 1;
 
                // Decrease the bit position
                x--;
            }
        }
 
        // Size of the largest possible subset
        int max = Integer.MIN_VALUE;
 
        for (int i = 0; i < 32; i++) {
            max = Math.max(max, bit[i]);
        }
 
        System.out.println(max);
    }
   
  // Driver code
    public static void main (String[] args)
    {
        int arr[] = {7, 13, 8, 2, 3};
        int N = arr.length;
        largestSubset(arr, N);
    }
}
 
// This code is contributed by Dharanendra L V.


Python3
# Python 3 program for the above approach
 
# Function to find the largest possible
# subset having Bitwise AND positive
def largestSubset(a, N):
    # Stores the number of set bits
    # at each bit position
    bit = [0 for i in range(32)]
 
    # Traverse the given array arr[]
    for i in range(N):
        # Current bit position
        x = 31
 
        # Loop till array element
        # becomes zero
        while(a[i] > 0):
            # If the last bit is set
            if (a[i] & 1 == 1):
 
                # Increment frequency
                bit[x] += 1
 
            # Divide array element by 2
            a[i] = a[i] >> 1
 
            # Decrease the bit position
            x -= 1
 
    # Size of the largest possible subset
    print(max(bit))
 
# Driver Code
if __name__ == '__main__':
    arr = [7, 13, 8, 2, 3]
    N = len(arr)
    largestSubset(arr, N)
 
    # This code is contributed by ipg016107.


C#
// C# program for the above approach
using System;
class GFG {
 
    static void largestSubset(int[] a, int N)
    {
 
        // Stores the number of set bits
        // at each bit position
        int[] bit = new int[32];
 
        // Traverse the given array arr[]
        for (int i = 0; i < N; i++) {
 
            // Current bit position
            int x = 31;
 
            // Loop till array element
            // becomes zero
            while (a[i] > 0) {
 
                // If the last bit is set
                if ((int)(a[i] & 1) == (int)1) {
 
                    // Increment frequency
                    bit[x]++;
                }
 
                // Divide array element by 2
                a[i] = a[i] >> 1;
 
                // Decrease the bit position
                x--;
            }
        }
 
        // Size of the largest possible subset
        int max = Int32.MinValue;
 
        for (int i = 0; i < 32; i++) {
            max = Math.Max(max, bit[i]);
        }
 
        Console.WriteLine(max);
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        int[] arr = { 7, 13, 8, 2, 3 };
        int N = arr.Length;
        largestSubset(arr, N);
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
3

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