Problem · Array

Fixed-Length Combination Sum

Learn this problem
MediumPinterest logoPinterestFULLTIMEPHONE SCREEN

Problem statement

Given integers k and target, return every combination of exactly k distinct integers from 1 through 9 whose sum is target.

  • Each value may be selected at most once.
  • Values within a combination must be increasing.
  • Return the combinations in lexicographic order.

Function

fixedLengthCombinationSum(k: int, target: int) → int[][]

Examples

Example 1

k = 3target = 7return = [[1,2,4]]

The only three distinct values from 1 through 9 that sum to 7 are 1, 2, and 4.

Example 2

k = 3target = 9return = [[1,2,6],[1,3,5],[2,3,4]]

These are all increasing three-value combinations with sum 9.

Example 3

k = 4target = 1return = []

The smallest sum of four distinct positive values is greater than 1.

Constraints

  • 1 <= k <= 9.
  • 1 <= target <= 60.

More Pinterest problems

drafts saved locally
public int[][] fixedLengthCombinationSum(int k, int target) {
    // Write your code here.
}
k3
target7
expected[[1,2,4]]
checking account