Find the size of Largest Subset with positive Bitwise AND
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++
#include <bits/stdc++.h>
using namespace std;
void largestSubset( int a[], int N)
{
int bit[32] = { 0 };
for ( int i = 0; i < N; i++) {
int x = 31;
while (a[i] > 0) {
if (a[i] & 1 == 1) {
bit[x]++;
}
a[i] = a[i] >> 1;
x--;
}
}
cout << *max_element(bit, bit + 32);
}
int main()
{
int arr[] = { 7, 13, 8, 2, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
largestSubset(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void largestSubset( int a[], int N)
{
int bit[] = new int [ 32 ];
for ( int i = 0 ; i < N; i++) {
int x = 31 ;
while (a[i] > 0 ) {
if (( int )(a[i] & 1 ) == ( int ) 1 ) {
bit[x]++;
}
a[i] = a[i] >> 1 ;
x--;
}
}
int max = Integer.MIN_VALUE;
for ( int i = 0 ; i < 32 ; i++) {
max = Math.max(max, bit[i]);
}
System.out.println(max);
}
public static void main (String[] args)
{
int arr[] = { 7 , 13 , 8 , 2 , 3 };
int N = arr.length;
largestSubset(arr, N);
}
}
|
Python3
def largestSubset(a, N):
bit = [ 0 for i in range ( 32 )]
for i in range (N):
x = 31
while (a[i] > 0 ):
if (a[i] & 1 = = 1 ):
bit[x] + = 1
a[i] = a[i] >> 1
x - = 1
print ( max (bit))
if __name__ = = '__main__' :
arr = [ 7 , 13 , 8 , 2 , 3 ]
N = len (arr)
largestSubset(arr, N)
|
C#
using System;
class GFG {
static void largestSubset( int [] a, int N)
{
int [] bit = new int [32];
for ( int i = 0; i < N; i++) {
int x = 31;
while (a[i] > 0) {
if (( int )(a[i] & 1) == ( int )1) {
bit[x]++;
}
a[i] = a[i] >> 1;
x--;
}
}
int max = Int32.MinValue;
for ( int i = 0; i < 32; i++) {
max = Math.Max(max, bit[i]);
}
Console.WriteLine(max);
}
public static void Main( string [] args)
{
int [] arr = { 7, 13, 8, 2, 3 };
int N = arr.Length;
largestSubset(arr, N);
}
}
|
Javascript
<script>
function largestSubset(a, N)
{
let bit = new Array(32).fill(0);
for (let i = 0; i < N; i++) {
let x = 31;
while (a[i] > 0) {
if (a[i] & 1 == 1) {
bit[x]++;
}
a[i] = a[i] >> 1;
x--;
}
}
let max = Number.MIN_VALUE;
for (let i = 0; i < 32; i++) {
max = Math.max(max, bit[i]);
}
document.write(max);
}
let arr = [7, 13, 8, 2, 3];
let N = arr.length;
largestSubset(arr, N);
</script>
|
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)
Share your thoughts in the comments
Please Login to comment...