 Open in App
Not now

# Find the size of Largest Subset with positive Bitwise AND

• Difficulty Level : Medium
• Last Updated : 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: 3
Explanation:
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++

 `// 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 = { 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);` `    ``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``;`   `        ``// 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)

• [(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)

My Personal Notes arrow_drop_up
Related Articles