Generating Power Set of Unique Integers

Day - 59 Subsets The problem - Given array of unique integers, return all possible subsets (power set). Solution must not contain duplicate subsets. Example : nums = [1,2,3] → [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] Brute Force - Generate all combinations iteratively, still exponential, but less elegant than recursion. Approach Used - •) Call createSubset(nums, 0, res, subset). •) Base case, if index == nums.length, add current subset to result. •) Recursive First case, include nums[index], add to subset, recurse with index+1, backtrack (remove). •) Recursive Second case, exclude nums[index]: recurse with index+1 (don't add). Complexity - Time - O(n × 2^n),each element has 2 choices. Space - O(n), recursion depth. Note - For each element, make two choices, include it or exclude it. Recursively build all combinations #DSA #Java #SoftwareEngineering #InterviewPrep #LearnToCode #CodeDaily #ProblemSolving

  • text

To view or add a comment, sign in

Explore content categories