FastPrepSubsets

Subsets

Temu logoTemu● MediumFULLTIMEPHONE SCREEN

Problem statement

You are given an integer array nums whose numbers are all different. Return every subset of nums, including the empty subset. Each subset appears exactly once.

So that the answer can be checked, return the subsets in this order:

  • Inside a subset, keep the numbers in the same order as in nums.
  • List shorter subsets before longer ones.
  • For two subsets of the same size, compare the positions of their numbers in nums. The subset with the smaller first position comes first. If the first positions are equal, compare the second positions, and so on.

An array of length n has 2^n subsets.

Function

subsets(nums: int[]) → int[][]

Examples

Example 1

nums = [1,2,3]return = [[],[1],[2],[3],[1,2],[1,3],[2,3],[1,2,3]]

This is the example from the report. The empty subset comes first, then the three subsets of size one, then the three of size two, then all three numbers.

Example 2

nums = [0]return = [[],[0]]

An array with one number has two subsets: the empty subset and the number itself.

Example 3

nums = [3,-1,2]return = [[],[3],[-1],[2],[3,-1],[3,2],[-1,2],[3,-1,2]]

Each subset keeps the order of nums, so it is [3,-1], not [-1,3]. Subsets of the same size follow the positions of their numbers: [3] uses position 0, [-1] position 1 and [2] position 2.

Constraints

  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10
  • All the numbers in nums are different.

More Temu problems

See Temu hiring insights
Saved
public int[][] subsets(int[] nums) {
    // Write your code here.
}
Checking account…