Open In App

Find the size of Largest Subset with positive Bitwise AND

Last Updated : 02 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 <bits/stdc++.h>
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




<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>


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)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads