40. Combination Sum II - Detailed Explanation

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Problem Statement

Given a collection of candidate numbers candidates (without duplicates) and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may only use each candidate once. The solution set must not contain duplicate combinations.

Examples

Example 1

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

Example 2

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

Constraints

  • 1 ≤ candidates.length ≤ 100
  • 1 ≤ candidates[i] ≤ 50
  • All elements of candidates are integers (may contain duplicates)
  • 1 ≤ target ≤ 500

Hints

  • Sort candidates first to help skip duplicates.
  • Use backtracking, advancing an index so each number is used at most once.
  • When you see the same value at the same recursive depth, skip it to avoid duplicate sequences.

Approach 1 Backtracking with Pruning

Idea

We explore each number in sorted order, decide to include it or skip it, and recurse on the remaining target. By sorting we can easily detect and skip over duplicates at the same decision level.

Steps

  1. Sort candidates.
  2. Define a recursive function dfs(start, remaining, path):
    • If remaining == 0, record a copy of path.
    • Otherwise, for i from start to len(candidates)-1:
      • If candidates[i] > remaining, break (further ones are too big).
      • If i > start and candidates[i] == candidates[i-1], continue (skip duplicate).
      • Append candidates[i] to path, recurse dfs(i+1, remaining - candidates[i], path), then pop.
  3. Call dfs(0, target, []) and return collected results.

Code (Python)

Python3
Python3

. . . .

Code (Java)

Java
Java

. . . .

Complexity Analysis

  • Time ≈ O(2ⁿ) in the worst case due to exploring subsets, but pruning and duplicate‐skipping reduce the practical load.
  • Space O(n) recursion depth plus O(#solutions × n) to store results.

Step‑by‑Step Walkthrough and Visual Example

For candidates = [1,1,2,5,6,7,10], target = 8 after sorting:

  1. Start at 1 (index 0): choose it → remaining 7 → recurse start 1
  2. At index 1 also 1: choose second 1 → remaining 6 → recurse start 2
  3. At index 2 2: choose → remaining 4 … eventually path [1,1,6] → record.
  4. Backtrack to explore [1,2,…], skip duplicate 1 at index 1 when start 1, etc.
  5. Explore [2,6], [1,7], [1,2,5], [1,7], [2,6], and so on, skipping repeated values at each level.

Common Mistakes

  • Forgetting to sort, so duplicate‐skipping (i>start && cand[i]==cand[i-1]) fails.
  • Using dfs(i,…) instead of dfs(i+1,…), allowing reuse of the same element.
  • Not breaking when cand[i] > rem, leading to unnecessary exploration.

Edge Cases

  • Empty candidates → return empty list.
  • All candidates larger than target → return empty list.
  • Many duplicates in candidates but no combination sums to target.

Alternative Variations

  • Combination Sum (LeetCode 39) allows unlimited use of each candidate.
  • Combination Sum III (LeetCode 216) fixes the number of elements to choose.
  • Subset Sum Count: count the number of ways instead of enumerating them.
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.
;