Ctrl K

Every Topping Combo

Medium
A coffee shop lets you add any toppings from a list to your drink. Each topping is identified by an integer code, and the codes in the array toppings are all distinct. Return every distinct set of toppings you could pick, including the empty set and the set with all of them. A set is defined only by which toppings it contains, so the answer must not list the same set twice. The sets may come in any order, and so may the codes inside each set.
Example 1
toppings=[4, 7] → [[], [7], [4], [4, 7]]

You can take nothing, only 4, only 7, or both, which makes four options.

Example 2
toppings=[-1] → [[], [-1]]

A single topping gives two sets: the empty one and the topping on its own.

Example 3
toppings=[3, 0, 9] → [[], [9], [0], [0, 9], [3], [3, 9], [3, 0], [3, 0, 9]]

Three toppings give 2³ = 8 sets, from empty to all three.

Constraints
  • 1 ≤ toppings.length ≤ 10
  • -1000 ≤ toppings[i] ≤ 1000
  • All codes in toppings are distinct
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsAll Distinct Card Hands MediumSquare Frame From Sticks MediumAll Non-Falling Subsequences MediumSeating by Divisibility MediumCheapest Basket with Bundles Medium
loading Python…
Input
toppings=[4, 7]
Expected
[[], [7], [4], [4, 7]]

Run checks the first 3 tests in your browser. Submit checks all 91 tests on the server, hidden ones included.