Skip to main content
Back to problems
Leetcode
Medium
Arrays
Backtracking
Combinatorics
Google
Combination Sum III

Find all unique combinations of k distinct numbers from 1 to 9 that add up to n.

Acceptance 0%
Problem Statement

Combination Sum III

Choose exactly k distinct numbers from the set {1, 2, 3, 4, 5, 6, 7, 8, 9} so that their sum is exactly n.

Return all valid combinations. Each number may be used at most once, and combinations should not contain duplicate sets of numbers.

You may return the answers in any order.

Input Format

  • Two integers k and n.
  • k is the number of values to choose.
  • n is the target sum.

Output Format

  • Return a list of all distinct combinations.
  • Each combination should contain exactly k numbers in strictly increasing order.

Constraints

  • 1 <= k <= 9
  • 1 <= n <= 60
  • Numbers can only be chosen from 1 to 9
  • Each number can be used at most once
Examples
Sample cases returned by the problem API.

Example 1

Input

k = 3, n = 7

Output

[[1,2,4]]

Explanation

The only 3-number combination from 1..9 that sums to 7 is [1,2,4].

Example 2

Input

k = 3, n = 9

Output

[[1,2,6],[1,3,5],[2,3,4]]

Explanation

These are the valid increasing combinations of three distinct numbers from 1..9 whose sum is 9.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.