Skip to main content
mediumBlind 75

Combination Sum

Given an array of distinct integers and a target Return all unique combinations that sum to the target Each number can be used unlimited times Example: candidates = [2,3,6,7], target = 7 => [[2,2,3],[7]]

Given an array of distinct positive integers candidates and a positive integer target, return every unique combination of candidates whose values sum to target. Each candidate may be used any number of times, and two combinations are the same if they use the same numbers the same number of times. Return the combinations in any order, or an empty list if there are none.

Examples

You write

combination_sum(candidates: List[int], target: int)

  1. Example 1

    Input
    candidates = [2,3,6,7], target = 7
    returns
    [[2,2,3],[7]]
  2. Example 2

    Input
    candidates = [2,3,5], target = 8
    returns
    [[2,2,2,2],[2,3,3],[3,5]]

The pattern behind it

Arrays & Hashing

I reach for it when

  • You catch yourself scanning the array again for every element.
  • The problem asks about duplicates, pairs, counts or groups.

Say this before you type. Say what the key is and what the value remembers, before you write the loop.

All 127 Arrays & Hashing problems

Backtracking

I reach for it when

  • The question asks for all combinations, permutations or placements.
  • You make a choice, explore it, and may need to take it back.

Say this before you type. Say the choice, the stopping rule, and the earliest moment you can prune.

All 15 Backtracking problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Find All Combinations Summing to Targetmedium

Prep guides that list this problem

Our company tags come from public interview write-ups. None of these is a confirmed question.