Combination Sum II
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.
candidates, a list of numbers (repeats allowed, each from 1 to 50), and an integer target.
Every different combination of candidates whose sum is target. Each combination is a list of numbers.
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.
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.
- • 1 ≤ candidates.length ≤ 100
- • 1 ≤ candidates[i] ≤ 50
- • 1 ≤ target ≤ 30
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Take or skip every number; keep each subset that hits the target, sorted and stored in a set
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
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:
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.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Backtracking translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.