2Brute2Best
🔥0

Combination Sum II

Medium·LeetCode #40 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You get a list of numbers called candidates, which may contain repeats, and a number target. Find every combination of numbers from candidates that adds up to exactly target.

Each number in candidates can be used at most once in a combination. Two combinations count as the same when they use the same numbers the same number of times, so the answer never lists the same combination twice.

Return all the different combinations. The order of the answers does not matter.

Input

candidates, a list of numbers (repeats allowed, each from 1 to 50), and an integer target.

Output

Every different combination of candidates whose sum is target. Each combination is a list of numbers.

Example 1
Input: candidates = [10, 1, 2, 7, 6, 1, 5], target = 8
Output: [[1, 1, 6], [1, 2, 5], [1, 7], [2, 6]]

Each combination adds up to 8. The input has two 1s, so [1, 1, 6] uses both of them. [1, 2, 5] and [1, 7] use one of the 1s each.

Example 2
Input: candidates = [2, 5, 2, 1, 2], target = 5
Output: [[1, 2, 2], [5]]

1 + 2 + 2 = 5 and 5 = 5. The input has three 2s, but [1, 2, 2] needs only two of them, and it is listed once.

Constraints
  • • 1 ≤ candidates.length ≤ 100
  • • 1 ≤ candidates[i] ≤ 50
  • • 1 ≤ target ≤ 30
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“each number can be used at most once”
→ No reuse → recurse on i + 1, not i.
“the input can contain repeated values”
→ Repeats make the same answer appear twice → sort, then skip repeats at the same level.
“the answer must not repeat a combination”
→ Remove the repeats by a rule while searching, not with a set at the end.
03

Approach Complexity Ladder

Brute Force — take or skip each number, then remove duplicate answersO(2^N · N log N)O(N)

Take or skip every number; keep each subset that hits the target, sorted and stored in a set

Optimal — sort, then skip repeats at the same levelO(N log N + 2^N · N)O(N)

Sort; use each index at most once (recurse on i + 1); skip a repeated value that is not the first copy at this level; break when too big

Brute Force Intuition

For every number, decide take or skip. A finished choice whose sum equals target is an answer. Sort each answer and put it in a set, so the same answer found in two ways counts once. Here, the same answer shows up more than once because taking the first 1 and taking the second 1 give the same numbers.

Recognition rule for this sub-pattern:

Target-driven. Reuse allowed → recurse on the same index; no reuse → next index and skip duplicates.
Remember this · Backtracking
Trigger
"All subsets / permutations / combinations", place items under rules (N-Queens, Sudoku), find a path in a grid.
Think
At each step, what are my choices, and how do I undo a choice?
Key question
What is my state, when is it complete, and when do I prune?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Backtracking translate into production architectures and low-level memory mechanics.