Power Set Bit Manipulation

Bit Manipulation Problems Medium
  • The concept of generating all possible subsets of a set (also known as power set) is commonly used in various realms of software development, particularly in machine learning
  • For example, in feature selection for a machine learning model, it's crucial to explore different combinations of features (subsets) to see which ones contribute the most to the predictive model's accuracy
  • This is essentially generating the power set of features
  • Similarly, it is used in big data analytics, search engines, and data mining for various computation-related tasks involving sets of data

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