Combination Sum III - Problem

Imagine you're a mathematician tasked with finding all possible ways to select k distinct digits from 1 through 9 that add up to exactly n. This is a classic combinatorial problem that tests your ability to explore all valid combinations systematically.

The Challenge:

  • You can only use digits 1, 2, 3, 4, 5, 6, 7, 8, 9
  • Each digit can be used at most once in any combination
  • You must find exactly k numbers that sum to n
  • Return all valid combinations (order doesn't matter)

Example: If k = 3 and n = 7, you need 3 distinct digits that sum to 7. The only solution is [1, 2, 4] because 1 + 2 + 4 = 7.

Input & Output

example_1.py โ€” Basic case
$ Input: k = 3, n = 7
โ€บ Output: [[1,2,4]]
๐Ÿ’ก Note: We need 3 distinct numbers from 1-9 that sum to 7. Only [1,2,4] works: 1+2+4=7
example_2.py โ€” Multiple solutions
$ Input: k = 3, n = 9
โ€บ Output: [[1,2,6],[1,3,5],[2,3,4]]
๐Ÿ’ก Note: Three different combinations work: 1+2+6=9, 1+3+5=9, and 2+3+4=9
example_3.py โ€” No solution
$ Input: k = 4, n = 1
โ€บ Output: []
๐Ÿ’ก Note: Impossible: we need 4 distinct positive digits, minimum sum would be 1+2+3+4=10, which exceeds target 1

Visualization

Tap to expand
Backtracking: Building Combinations Step by Step1. Start Emptycombo = [], sum = 02. Try Digit 1combo = [1], sum = 13. Try Digit 2combo = [1,2], sum = 34. Try Digit 4combo = [1,2,4], sum = 7 โœ“Pruning Examples (k=3, n=7):โŒ [1,2,5] โ†’ sum = 8 > 7 (prune branch)โŒ [1,8] โ†’ remaining digits can't make sum = 7 (min: 1+8+9=18)โœ… [1,2,4] โ†’ sum = 7, length = 3 (valid solution)โŒ [7,8,9] โ†’ sum = 24 > 7 (prune early)Key Insight: Early PruningInstead of generating all C(9,k) combinations, we:โ€ข Stop immediately when sum > targetโ€ข Stop when remaining digits can't reach targetโ€ข Build combinations incrementally (avoid duplicates)Result: Much faster than brute force!
Understanding the Visualization
1
Start with Empty Set
Begin with no digits selected, sum = 0
2
Try Each Digit
For each position, try adding digits from current minimum to 9
3
Check Constraints
Verify: sum โ‰ค n, combo length โ‰ค k, still possible to reach target
4
Backtrack if Invalid
If constraints violated, remove last digit and try next option
5
Found Solution
When k digits selected and sum = n, add to results
Key Takeaway
๐ŸŽฏ Key Insight: Backtracking with early pruning transforms an exponential search space into a much more manageable exploration by eliminating impossible branches before fully exploring them.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(2^9)

In worst case, we explore all subsets of 9 digits, but pruning makes it much faster in practice

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Recursion depth is at most k, and we store current combination of size k

n
2n
โœ“ Linear Space

Constraints

  • 2 โ‰ค k โ‰ค 9
  • 1 โ‰ค n โ‰ค 60
  • Only digits 1 through 9 can be used
  • Each digit can be used at most once per combination
Asked in
Google 25 Amazon 20 Meta 15 Microsoft 12
58.0K Views
Medium Frequency
~15 min Avg. Time
2.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen