Power Set

Recursion Implementation Problems Easy
  • The problem of finding all possible subsets from a set, also known as the power set, is an important concept in many areas of software development
  • In database technologies, such as SQL, this concept is used in designing queries that require combinations of data, such as generating all combinations of products that can be bundled together
  • In Machine Learning, power sets are used in feature selection, where combinations of different features are tested to see which set gives the best predictive accuracy
  • Meanwhile in Software Testing, generating all possible subsets of a feature set aids in creating exhaustive test cases which helps find potential bugs or issues

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 recursion to build subsets by deciding for each element whether to include it in the current subset.
  • "At each recursive step: Add the current subset to the result. Recursively add subsets including the next element."

Company Tags

Wayfair Dropbox Red Hat Swiggy Bain & Company Roche Zynga Nutanix Morgan Stanley Freshworks Visa MongoDB Seagate Technology Bloomberg Bungie Optum Boston Consulting Group Micron Technology Splunk HCL Technologies Activision Blizzard DoorDash Philips Healthcare Riot Games JPMorgan Chase TCS Cognizant Accenture Infosys Capgemini Wipro