Loading content...
You are given two integers k and n. Your task is to discover all valid combinations of exactly k distinct digits (from the set 1 through 9) that add up to exactly n.
Each digit in the set {1, 2, 3, 4, 5, 6, 7, 8, 9} may appear at most once in any combination. Additionally, every combination must be distinct—meaning no two combinations in the result should contain the exact same set of digits, regardless of their arrangement.
Return a list containing all such valid combinations. The combinations may be returned in any order, and within each combination, the digits may also appear in any order.
k digits.n.k = 3, n = 7[[1,2,4]]The only combination of 3 distinct digits from 1-9 that sums to 7 is [1, 2, 4], since 1 + 2 + 4 = 7. Other potential triplets like [1, 1, 5] are invalid because digit repetition is not allowed.
k = 3, n = 9[[1,2,6],[1,3,5],[2,3,4]]There are three valid combinations of 3 distinct digits that sum to 9: • 1 + 2 + 6 = 9 • 1 + 3 + 5 = 9 • 2 + 3 + 4 = 9 All other triplets either exceed the target sum or require duplicate digits.
k = 4, n = 1[]No valid combination exists. The minimum possible sum using 4 distinct digits from 1-9 is 1 + 2 + 3 + 4 = 10. Since 10 > 1, it is impossible to form any 4-digit combination that sums to just 1.
Constraints