Quick Overview

Generate every subset of an array of distinct integers, with each subset listed in ascending order and the whole list sorted lexicographically. Tests building the power set by iteration or backtracking, copying partial results safely, and reasoning about exponential output size.

Generate all subsets of an array of distinct integers

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an integer array `nums` whose elements are all distinct, return every subset of `nums` (the power set). Each subset must appear exactly once, and the empty subset counts as a subset. ### Function Signature ```python def subsets(nums: list[int]) -> list[list[int]]: ``` ### Rules - List the elements inside each subset in ascending order. - Order the subsets lexicographically, the way Python compares lists: element by element from the left, and a list that is a prefix of a longer list comes first. The empty subset is therefore always first. - The order of `nums` in the input does not change the output. ### Constraints - `1 <= len(nums) <= 10` - `-10 <= nums[i] <= 10` - All elements of `nums` are distinct. ### Examples ```text Input: nums = [1, 2, 3] Output: [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]] ``` Three distinct elements give 8 subsets, one for each way to include or skip each element. `[1]` comes before `[1, 2]` because it is a prefix, and `[1, 2, 3]` comes before `[1, 3]` because 2 < 3 at the second position. ```text Input: nums = [0] Output: [[], [0]] ``` One element gives two subsets: the empty one and the element on its own. ```text Input: nums = [3, -1] Output: [[], [-1], [-1, 3], [3]] ``` The two-element subset is written `[-1, 3]` because elements inside a subset are ascending whatever the input order. It sorts before `[3]` because -1 < 3 at the first position.

Overview: Generate every subset of an array of distinct integers, with each subset listed in ascending order and the whole list sorted lexicographically. Tests building the power set by iteration or backtracking, copying partial results safely, and reasoning about exponential output size.

You are given an integer array `nums` whose elements are all distinct. Return every subset of `nums`, that is, its power set. Each subset must appear exactly once, and the empty subset counts as a subset. The result is a list of lists in one fixed order: - The elements inside each subset are listed in ascending order. - The subsets are ordered lexicographically, the way Python compares lists. Two subsets are compared element by element from the left, as numbers, and the first position where they differ decides: the subset with the smaller value at that position comes first. If one subset is a prefix of a longer one, the shorter one comes first, so the empty subset is always first. - The order of `nums` in the input does not change the output. An array of length n has 2^n subsets, so the result holds between 2 and 1024 lists. Every value fits in a 32-bit signed integer. ### Example 1 ```text Input: nums = [1, 2, 3] Output: [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]] ``` Three distinct elements give 8 subsets, one for each way to include or skip each element. `[1]` comes before `[1, 2]` because it is a prefix of it, and `[1, 2, 3]` comes before `[1, 3]` because 2 < 3 at the second position. ### Example 2 ```text Input: nums = [3, -1] Output: [[], [-1], [-1, 3], [3]] ``` The two-element subset is written `[-1, 3]` because elements inside a subset are ascending whatever the input order. It comes before `[3]` because -1 < 3 at the first position. ### Constraints - `1 <= len(nums) <= 10` - `-10 <= nums[i] <= 10` - All elements of `nums` are distinct.

Constraints

  • 1 <= len(nums) <= 10
  • -10 <= nums[i] <= 10
  • All elements of nums are distinct.

Examples

Input: ([0],)

Expected Output: [[], [0]]

Explanation: Singleton zero from the source's second example: the empty subset first, then the element alone.

Input: ([-7],)

Expected Output: [[], [-7]]

Explanation: Singleton negative value still gives two subsets with the empty one first.

Hints

  1. The input order never changes the answer, so you may rearrange `nums` into whatever order makes the rest easier.
  2. With n distinct values, every subset matches one yes or no choice per value, which gives 2^n subsets in total.
  3. Check your order against Example 1: `[1, 2]` and `[1, 2, 3]` both come before `[1, 3]`, and every subset that starts with 1 comes before `[2]`.

Power Set in Lexicographic Order

mediumGeneralSoftware Engineer

You are given an integer array nums whose elements are all distinct. Return every subset of nums, that is, its power set. Each subset must appear exactly once, and the empty subset counts as a subset.

The result is a list of lists in one fixed order:

  • The elements inside each subset are listed in ascending order.
  • The subsets are ordered lexicographically, the way Python compares lists. Two subsets are compared element by element from the left, as numbers, and the first position where they differ decides: the subset with the smaller value at that position comes first. If one subset is a prefix of a longer one, the shorter one comes first, so the empty subset is always first.
  • The order of nums in the input does not change the output.

An array of length n has 2^n subsets, so the result holds between 2 and 1024 lists. Every value fits in a 32-bit signed integer.

Example 1

Input: nums = [1, 2, 3]
Output: [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]

Three distinct elements give 8 subsets, one for each way to include or skip each element. [1] comes before [1, 2] because it is a prefix of it, and [1, 2, 3] comes before [1, 3] because 2 < 3 at the second position.

Example 2

Input: nums = [3, -1]
Output: [[], [-1], [-1, 3], [3]]

The two-element subset is written [-1, 3] because elements inside a subset are ascending whatever the input order. It comes before [3] because -1 < 3 at the first position.

Constraints

  • 1 <= len(nums) <= 10
  • -10 <= nums[i] <= 10
  • All elements of nums are distinct.

Examples

Example 1
Input
([0],)
Output
[[], [0]]
Notes
Singleton zero from the source's second example: the empty subset first, then the element alone.
Example 2
Input
([-7],)
Output
[[], [-7]]
Notes
Singleton negative value still gives two subsets with the empty one first.

Constraints

  • •1 <= len(nums) <= 10
  • •-10 <= nums[i] <= 10
  • •All elements of nums are distinct.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def subsets(nums: list[int]) -> list[list[int]]:
Sample call
subsets([0])
Language
Case 1
Input
([0],)
Expected
[[], [0]]
Case 2
Input
([-7],)
Expected
[[], [-7]]

Browse More Questions

Show the approach

Approach

Sort a copy of nums in ascending order. A subset written with ascending elements is then an increasing sequence of picks from the sorted array, so the task is to list those sequences in lexicographic order, and a depth-first build produces them in exactly that order. Each recursive step receives the subset built so far and the first index it may still use. It records the current subset, then for every index i from that start to the end it appends values[i], recurses with start i + 1, and removes values[i] again.

Whenever a step runs with subset S and start index s, every element of S is smaller than values[s], so any appended value keeps S ascending, and the step records S followed by every subset that extends S with values taken from index s onward, each exactly once.

Each subset is recorded once, on the single path that picks its elements in increasing index order. The order is lexicographic because S is recorded before any of its extensions, so a prefix comes first, and all extensions whose next value is smaller are listed before any extension whose next value is larger, which is how the first differing position decides. Sorting first makes the result independent of the input order.

A single element x gives [[], [x]]. The sort has to compare numbers; a text sort would put -1 before -10 and 10 before 2. Generating subsets by size or in bitmask order places [2] before [1, 2], so those orders would need a final lexicographic sort. With n at most 10 the output holds at most 1024 subsets and the recursion is at most 11 levels deep.

Time complexity:
O(n * 2^n)
Space complexity:
O(n * 2^n)