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
def largestSubset ( a , N ) : NEW_LINE INDENT bit = [ 0 for i in range ( 32 ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT x = 31 NEW_LINE while ( a [ i ] > 0 ) : NEW_LINE INDENT if ( a [ i ] & 1 == 1 ) : NEW_LINE INDENT bit [ x ] += 1 NEW_LINE DEDENT a [ i ] = a [ i ] >> 1 NEW_LINE x -= 1 NEW_LINE DEDENT DEDENT print ( max ( bit ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 7 , 13 , 8 , 2 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE largestSubset ( arr , N ) NEW_LINE DEDENT