DSA Tracker

Medium

Combination Sum

A medium Backtracking problem included in Apna College, Love Babbar 450, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Backtracking
Sheets
3
Core for
0 roles
Platform
LeetCode

The problem

Given an array of distinct positive integers and a target sum, return all unique combinations where the chosen numbers sum to the target. The same number may be chosen from the array an unlimited number of times. Two combinations are unique if the frequency of at least one chosen number differs.

Example 1

Input
candidates = [2, 3, 6, 7], target = 7
Output
[[2, 2, 3], [7]]
Why
2+2+3=7 and 7=7. Both are valid combinations that sum to 7.

Example 2

Input
candidates = [2, 3, 5], target = 8
Output
[[2, 2, 2, 2], [2, 3, 3], [3, 5]]
Why
2+2+2+2=8, 2+3+3=8, and 3+5=8. These are all unique combinations summing to 8.

Constraints

  • 1 <= candidates.length <= 30
  • 1 <= candidates[i] <= 200
  • All elements of candidates are distinct
  • 1 <= target <= 500

How to think about it

Updated 2026-09-09

Unlimited reuse does not mean unrestricted ordering. Ordering causes duplicate permutations like [2, 3, 2] and [2, 2, 3]; freezing an index pointer and only allowing transitions to the current candidate or later candidates enforces a canonical non-decreasing choice order while preserving full freedom of repetition.

Approaches, worst first

  1. Unconstrained branching with set deduplication

    time O(n^(target / min)) · space O(target / min * n^(target / min))

    Allow any candidate to be chosen at each recursive step, backtracking when sum exceeds target, then sorting paths to filter out permutations via a set. Blows up exponentially due to visiting all factorial orderings of identical multisets.

  2. Forward index recursion

    time O(n^(target / min)) · space O(target / min)

    Recurse with an index parameter. From index i, branch on candidates[j] for j >= i, passing j (not j + 1) to allow reuse of the same number. Stops immediately when remaining target reaches zero or turns negative.

  3. Sort and prune earlyWrite this one

    time O(n^(target / min)) · space O(target / min)

    Sort candidates ascending before searching. In the exploration loop, if candidates[j] exceeds the remaining target, break out immediately instead of continuing the loop. This truncates hopeless subtrees across all remaining larger numbers.

Where people lose marks · 3
  • Passing i + 1 instead of i to the recursive call, which prevents candidates from being reused and misses valid combinations like [2, 2, 2, 2].
  • Subtracting without breaking when candidates are unsorted. A break statement inside the loop only prunes correctly if candidates are sorted in ascending order.
  • Using candidates[i] <= 0 logic or assuming zero is possible. Constraints state candidate values are at least 1, which guarantees recursion depth is strictly bounded.

The theory behind it

Backtracking — the ground this problem stands on. All Backtracking problems

What Backtracking is

Backtracking is an organized trial-and-error search through a maze of possibilities. You make a tentative choice, move forward to explore where that path leads, and if you hit a dead end or finish finding an answer, you back up and undo that choice. By cleaning up your changes before trying the next option, a single shared board or list is explored thoroughly without needing to clone full copies of your data at every turn.

When to reach for it

Reach for backtracking when a problem asks to generate all possible solutions, like all subsets, permutations, valid parentheses combinations, or word search paths on a board. Signals include puzzles with strict constraint rules, like placing eight non-attacking queens on a chessboard or solving a Sudoku grid. Whenever you must construct combinations step by step and abandon dead-end branches early before wasting time exploring impossible paths, use backtracking.

How the pattern works

Follow a three-step rhythm inside a loop: choose, explore, and unchoose. First, check if the current state satisfies your goal; if so, save a copy of it and return. Next, prune illegal moves immediately using constraint checks so unpromising branches are skipped. For each valid candidate, apply the move to your shared path or board, call the recursive function to explore deeper, and finally undo the move right after the call returns. Undoing restores the shared state so sibling choices start from a clean slate.

What each operation costs

OperationTime
generate all subsets of n elementsO(2^n)
generate all permutations of n elementsO(n!)
auxiliary recursion stack memory depthO(n)
What usually goes wrong with Backtracking
  • Adding a mutable path list directly to the final answers collection without creating a shallow copy, leaving every saved result empty once backtracking finishes.
  • Forgetting to undo a state change after the recursive call returns, contaminating subsequent branches with leftover moves from earlier paths.
  • Generating duplicate subsets or permutations by failing to sort the input array and skip adjacent identical elements during branch selection.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Backtracking problems

Problem set and role mapping as of .