Find the size of Largest Subset with positive Bitwise AND

• Last Updated : 07 Oct, 2021

Given an array arr[] consisting of N positive integers, the task is to find the largest size of the subset of the array arr[] with positive Bitwise AND.

Examples:

Input: arr[] = [7, 13, 8, 2, 3]
Output: 3
Explanation:
The subset having Bitwise AND positive is {13, 7, 3} is of length 3, which is of maximum length among all possible subsets.

Input: arr[] = [1, 2, 4, 8]
Output: 1

Approach: The given problem can be solved by counting the number of set bits at each corresponding bits position for all array elements and then the count of the maximum of set bits at any position is the maximum count of subset required because the Bitwise AND of all those elements is always positive. Follow the steps below to solve the given problem:

• Initialize an array, say bit[] of size 32 that stores the count of set bits at each ith bit position.
• Traverse the given array and for each element, say arr[i] increment the frequency of the ith bit in the array bit[] if the ith bit is set in arr[i].
• After the above steps, print the maximum of the array bit[] to print the maximum size of the subset.

Below is the implementation of the above approach:

C++

 // C++ program for the above approach #include using namespace std; // Function to find the largest possible// subset having Bitwise AND positivevoid largestSubset(int a[], int N){    // Stores the number of set bits    // at each bit position    int bit = { 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 Codeint main(){    int arr[] = { 7, 13, 8, 2, 3 };    int N = sizeof(arr) / sizeof(arr);    largestSubset(arr, N);     return 0;}

Java

 // Java program for the above approachimport 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;         // 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 positivedef 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 Codeif __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 approachusing System;class GFG {     static void largestSubset(int[] a, int N)    {         // Stores the number of set bits        // at each bit position        int[] bit = new int;         // 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


Output:
3

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up