Browse Curriculum

Subsets (Power Set)

Given an integer array nums of unique elements, return all possible subsets (the power set). The solution set must not contain duplicate subsets.

Subsets Generation

Ready

See the Logic in Motion
Stop memorizing code. Unlock the full interactive visualizer to master the logic step-by-step.
Unlock VisualizerPREMIUM FEATURE

Intuition

Think of it as a series of decisions for each element: Include it in the current subset, or Exclude it.

If you have [1, 2, 3], for '1' you say Yes or No. Then for '2', you say Yes or No. This forms a binary decision tree.

Concept

Key Concepts:

  • Backtracking: Explore one branch (e.g., include element), then backtrack to explore the other (exclude element).
  • Decision Tree: At each step, we branch into two paths.
  • Power Set: A set of size N has 2^N subsets.

How it Works

  1. Start with an empty subset [].
  2. For each element in the input array:
  3. Include the element: Add it to the current path, move to next index.
  4. Exclude the element: Don't add it, move to next index.
  5. Base Case: When index equals array length, add current path to results.

Step-by-Step Breakdown

Input: [1, 2]

  • Start: []
  • Index 0 (Element 1):
  • - Include: [1] -> Recurse Index 1
  • -- Index 1 (Element 2):
  • --- Include: [1, 2] -> Done
  • --- Exclude: [1] -> Done
  • - Exclude: [] -> Recurse Index 1
  • -- Index 1 (Element 2):
  • --- Include: [2] -> Done
  • --- Exclude: [] -> Done

When to Use

  • Generating all combinations/subsequences.
  • Solving problems like "Knapsack" (brute force), "Subset Sum".

When NOT to Use

  • When you only need the **number** of subsets (use math 2^N).
  • When input size is large (N > 20) due to exponential complexity.

How to Identify

"Find all subsets", "Power Set", "Combinations of any length".

Stop Guessing, Start Mastering.

Build the FAANG intuition. Master this pattern with optimized implementations, visual dry runs, and our curated collection of high-yield problems.

Start Your Premium Prep