1325. Subsets Generation Using Bitmask

MediumBit ManipulationBitmaskSubsetsEnumeration

Given an array of distinct integers, generate every subset (the power set) using bitmask enumeration. Return the subsets ordered by their bitmask value from 0 to 2^n - 1, and within each subset keep the elements in their original array order. The empty subset comes first.

Input: A JSON object {"nums": [<distinct integers>]}.

Output: Return a list of subsets ordered by increasing bitmask value.

Examples

Example 1
Input: {"nums":[1,2]}
Output: [[],[1],[2],[1,2]]
Explanation: Masks 00,01,10,11 give [], [1], [2], [1,2].
Example 2
Input: {"nums":[7]}
Output: [[],[7]]
Explanation: Masks 0 and 1 give the empty subset and [7].

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →