Find the size of Largest Subset with positive Bitwise AND | C ++ program for the above approach ; Function to find the largest possible subset having Bitwise AND positive ; Stores the number of set bits at each bit position ; Traverse the given array arr [ ] ; Current bit position ; Loop till array element becomes zero ; If the last bit is set ; Increment frequency ; Divide array element by 2 ; Decrease the bit position ; Size of the largest possible subset ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 ; }