Find the size of Largest Subset with positive Bitwise AND

02 Jun, 2022
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.
Note : If there exist more than one such subsets then return size of only one subset.
Examples:
Input: arr[] = [7, 13, 8, 2, 3]Output: 3Explanation:The subsets having Bitwise AND positive are {7,13,3} and {7,2,3}  are 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. 
Illustration :
7 -->  00111
13 --> 01101
 8 --> 01000
 2 --> 00010
 3 --> 00011
       ------
       02233 <-- Evident BitWise AND bit(Most number of 1's in bit grid)

From above it is clearly evident that we can have maximum of 3 bitwise combinations 
where combinations are listed below as follows:         
{7,13,3}
{7,2,3}
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++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <bits/stdc++.h>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[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 Codeint main(){    int arr[] = { 7, 13, 8, 2, 3 };    int N = sizeof(arr) / sizeof(arr[0]);    largestSubset(arr, N);     return 0;}
// 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[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.
# 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# 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[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.
<script>       // JavaScript Program to implement       // the above approach        // Function to find the largest possible       // subset having Bitwise AND positive       function largestSubset(a, N)       {                   // Stores the number of set bits           // at each bit position           let bit = new Array(32).fill(0);            // Traverse the given array arr[]           for (let i = 0; i < N; i++) {                // Current bit position               let 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           let max = Number.MIN_VALUE;            for (let i = 0; i < 32; i++) {               max = Math.max(max, bit[i]);           }            document.write(max);       }        // Driver Code       let arr = [7, 13, 8, 2, 3];       let N = arr.length;       largestSubset(arr, N);     // This code is contributed by Potta Lokesh    </script>
3
Time Complexity: O(N)
[(32)* (length of array) where 32 is constant time, so as per recurrence tree the time complexity is of N order
Auxiliary Space: O(1)
lokeshpotta20
ipg2016107
dharanendralv23
ukasp
aditya942003patil
Bitwise-AND
setBitCount
subset
Arrays
Bit Magic
Mathematical
Arrays
Mathematical
Bit Magic
subset
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
Window Sliding Technique
Search, insert and delete in an unsorted array
What is Data Structure: Types, Classifications and Applications
Chocolate Distribution Problem
Bitwise Operators in C/C++
Left Shift and Right Shift Operators in C/C++
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Count set bits in an integer
How to swap two numbers without using a temporary variable?