FastPrepBounded Combination Sum with Signed Candidates

Bounded Combination Sum with Signed Candidates

Confluent logoConfluent● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

You are given distinct nonzero integer candidates, an integer target, and a positive maxLength. Candidate values may be positive or negative, and each candidate may be reused.

Return every unique nonempty combination whose sum is target and whose length is at most maxLength. Values inside each combination must be nondecreasing. Sort the returned combinations lexicographically.

Function

boundedCombinationSum(candidates: int[], target: int, maxLength: int) → int[][]

Examples

Example 1

candidates = [2,3,6,7]target = 7maxLength = 4return = [[2,2,3],[7]]

Both combinations sum to 7; candidate reuse permits [2,2,3].

Example 2

candidates = [-1,1]target = 0maxLength = 4return = [[-1,-1,1,1],[-1,1]]

The explicit length bound keeps signed-candidate reuse finite. The longer combination sorts first lexicographically because its second value is smaller.

Example 3

candidates = [-2,3]target = 1maxLength = 3return = [[-2,3]]

One use of each candidate reaches the target within the length limit.

Constraints

  • 1 <= candidates.length <= 8
  • -100 <= candidates[i] <= 100, every value is nonzero, and all values are distinct.
  • -500 <= target <= 500
  • 1 <= maxLength <= 8
  • The number of returned combinations is at most 5000.

More Confluent problems

See Confluent hiring insights
public int[][] boundedCombinationSum(int[] candidates, int target, int maxLength) {
    // Write your code here.
}
candidates[2,3,6,7]
target7
maxLength4
expected[[2,2,3],[7]]
Checking account…