Skip to content
Problem 40 · LeetCode 40(opens in a new tab)

Combination Sum II

MediumArrays

You are given a list of positive whole numbers, candidates, and a number target. Pick some of the candidates so that they add up to exactly target. Each position in the list can be picked at most once, but the list may hold the same value more than once.

Return every distinct combination. Two combinations made of the same values count as one, even if they use different positions. Return the combinations in any order, each one sorted from smallest to largest.

Example 1

Input:  candidates = [2, 1, 2, 4, 3], target = 5
Output: [[1, 2, 2], [1, 4], [2, 3]]
Why:    [2, 3] can use either 2, but it is listed once
2 more examples and the constraints

Example 2

Input:  candidates = [4, 4, 4], target = 8
Output: [[4, 4]]
Why:    any two of the three 4s give the same combination

Example 3

Input:  candidates = [6, 7], target = 5
Output: []
Why:    every candidate is bigger than 5

Constraints

  • 1 ≤ length of candidates ≤ 100
  • 1 ≤ each candidate ≤ 50
  • 1 ≤ target ≤ 30

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Backtracking + a setO(2ⁿ · n)
  2. Backtracking, skip repeatsO(2ⁿ · n)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 100 copies of 1, target 30Verdict
Backtracking + a setO(2ⁿ · n)
Correct, explodes on repeats
Backtracking, skip repeatsO(2ⁿ · n)
Best; each combination built once

n = number of candidates, r = number of distinct combinations.

Edge cases to test

  • Repeated values. [2, 1, 2, 4, 3] with target 5 lists [2, 3] once, not twice.
  • Copies used together. [1, 1, 1, 1, 1] with target 3 must give [1, 1, 1]; the skip may not block deeper copies.
  • Nothing fits. [6, 7] with target 5 returns an empty list.
  • Many large values. 100 candidates where only one is small enough: the early break keeps it fast.

Common mistakes

  • Skipping with j > 0 instead of j > start, which also forbids the same value one level deeper.
  • Recursing with j instead of j + 1, so a position is reused (that is Combination Sum I).
  • Appending the path itself instead of a copy, so every stored result ends up empty.

How to talk through it

  1. 1Clarify. Can values repeat? Can one position be used twice? Must each combination be sorted?
  2. 2Give the simple answer. Backtrack over positions and drop repeats with a set; say the set does not stop the wasted search.
  3. 3Find where repeats come from. Two equal values at the same depth start the same branch. Sorting puts them side by side.
  4. 4Code the skip. Skip j when j > start and the value equals the previous one; break once a value is too big.
  5. 5Test out loud. Run [1, 1, 1, 1, 1] with target 3 and a case with no answer before saying you are done.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?