Power Set Bit Manipulation

Bit Manipulation Problems Medium

Given an array of integers nums of unique elements. Return all possible subsets (power set) of the array.


Do not include the duplicates in the answer.

Examples:

Input : nums = [1, 2, 3]

Output : [ [ ] , [1] , [2] , [1, 2] , [3] , [1, 3] , [2, 3] , [1, 2 ,3] ]

Input : nums = [1, 2]

Output : [ [ ] , [1] , [2] , [1, 2] ]

Input : nums = [0]

Constraints

  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10

Hints

  • "Use a recursive function that explores two choices at each step: (a) Include the current element in the subset. (b) Exclude the current element."
  • "Represent each subset as a bitmask of length n, where each bit indicates whether an element is included (1) or excluded (0). Iterate through all numbers from 0 to 2^n−1. For each number, use its binary representation to construct the corresponding subset."

Company Tags

Riot Games Airbnb Oracle Docker Intel Qualcomm Medtronic Instacart eBay Morgan Stanley Epic Games HashiCorp Lyft PwC NVIDIA Flipkart Rakuten Roblox Zoho Teladoc Health Dropbox Splunk Walmart Salesforce DoorDash Google Microsoft Amazon Meta Apple Netflix Adobe