← DiffPush

Combination Sum 3

Standard Bar Recursion · Subsequences Pattern O(2^9) — the bench is fixed at nine candidates · O(k) recursion depth beyond the output

Seating a Jury Pool from Nine Fixed Candidates

A court coordinator seats a jury of exactly k members whose seniority scores add up to a required total n, drawing from a fixed candidate bench numbered 1 through 9. Each candidate serves at most once, and the roster must be complete — exactly k names, no fewer. The coordinator walks the bench once per slot: seat the current candidate and shrink the target, or pass on them for good.

Input: Two integers k (roster size) and n (required sum).

Output: Every k-member combination from 1..9 summing to n, sorted lexicographically by the verifier.

Constraints

Examples

Example 1

Input: {"k":3,"n":7}
Output: [[1,2,4]]
1 + 2 + 4 is the only three-member roster reaching 7.

Example 2

Input: {"k":3,"n":9}
Output: [[1,2,6],[1,3,5],[2,3,4]]
Three rosters: the smallest three plus two, or mid-weight trades around them.

Solve this in your browser →

Also on LeetCode ↗