</>

78. Subsets

Medium NeetCode 150
Backtracking Time O(2^N * N) Space O(2^N * N)
↗ Open on LeetCode Source: NeetCode 150

Quick review

Pattern
Backtracking
Time
O(2^N * N)
Space
O(2^N * N)

Standard Solution (C++)

SummaryGiven an integer array with all unique elements, return every possible subset (including the empty set and the full array itself), in any order.

Approach
  • Use backtracking: at each index, make a binary choice — include the current element or skip it — then recurse on the rest.
  • When the index reaches the end of the array, the current path represents one complete subset; record it.
  • Because every node in the recursion tree (not just the leaves) could be a valid subset, the include/exclude pattern naturally enumerates all 2^n subsets without duplicates.
  • The recursion depth is at most n, and each of the 2^n leaves takes O(n) time to copy into the result.
#include <vector>
using namespace std;

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        vector<vector<int>> result;
        vector<int> current;
        backtrack(nums, 0, current, result);
        return result;
    }

private:
    void backtrack(const vector<int>& nums, int start,
                   vector<int>& current, vector<vector<int>>& result) {
        // Every call state is a valid subset — record it immediately.
        result.push_back(current);

        for (int i = start; i < (int)nums.size(); ++i) {
            current.push_back(nums[i]);        // choose
            backtrack(nums, i + 1, current, result);
            current.pop_back();                // un-choose
        }
    }
};
Time O(2^n * n) Space O(2^n * n)

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)

class Solution {
public:
    void getSubset(vector<vector<int>>& res, vector<int>& cur, vector<int>& nums, int idx){
        if(idx==nums.size()){
            res.push_back(cur);
            return;
        }
        getSubset(res, cur, nums, idx+1);
        cur.push_back(nums[idx]);
        getSubset(res, cur, nums, idx+1);
        cur.pop_back();
    }
    vector<vector<int>> subsets(vector<int>& nums) {
        vector<vector<int>> res;
        vector<int> cur;

        getSubset(res, cur, nums, 0);
        return res;
    }
};

// Time Complexity: O(2^N * N). Why N? Because we need to copy the current subset to the result.
// Space Complexity: O(2^N * N), and O(N) for the auxiliary space.
Time O(2^N * N) Space O(2^N * N)

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground with a sample driver C++

Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.

C++ ▶ Runnable Auto-generated driver with sample input — unverified.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern