Find all distinct subsets of a given set using BitMasking Approach | Function to find all subsets of given set . Any repeated subset is considered only once in the output ; Run counter i from 000. . 0 to 111. . 1 ; consider each element in the set ; Check if jth bit in the i is set . If the bit is set , we consider jth element from set ; if subset is encountered for the first time If we use set < string > , we can directly insert ; consider every subset ; split the subset and print its elements ; Driver Code
def printPowerSet ( arr , n ) : NEW_LINE INDENT _list = [ ] NEW_LINE for i in range ( 2 ** n ) : NEW_LINE INDENT subset = " " NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( i & ( 1 << j ) ) != 0 : NEW_LINE INDENT subset += str ( arr [ j ] ) + " | " NEW_LINE DEDENT DEDENT if subset not in _list and len ( subset ) > 0 : NEW_LINE INDENT _list . append ( subset ) NEW_LINE DEDENT DEDENT for subset in _list : NEW_LINE INDENT arr = subset . split ( ' ▁ ' ) NEW_LINE for string in arr : NEW_LINE INDENT print ( string , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 12 , 12 ] NEW_LINE n = len ( arr ) NEW_LINE printPowerSet ( arr , n ) NEW_LINE DEDENT