Find the size of Largest Subset with positive Bitwise AND | 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
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 ) ;