Combination Sum with Unlimited Reuse
Updated
Problem statement
You are given an integer array candidates and an integer target. Return every distinct combination of candidate values that adds up to target.
The values in candidates are distinct positive integers. You may use each value as many times as you like. Two combinations count as the same if they use the same values the same number of times, in any order.
Write each combination with its values in nondecreasing order, from smallest to largest, with repeated values next to each other. Then sort the list of combinations in lexicographic order: compare two combinations value by value from the start, and at the first place they differ, the one with the smaller value comes first. Return an empty list when no combination exists.
Function
combinationSum(candidates: int[], target: int) → List<List<Integer>>Examples
Example 1
candidates = [2,3,6,7]target = 7return = [[2,2,3],[7]]One way is to use 2 twice and add 3, which reaches the target. The other way is 7 by itself. No other mix of the values works, so these are the two combinations.
Example 2
candidates = [2,3,5]target = 8return = [[2,2,2,2],[2,3,3],[3,5]]Exactly three combinations add up to 8. Each one lists its values from smallest to largest, and the three are sorted as described. No other way of picking values reaches the target.
Example 3
candidates = [2]target = 1return = []The only value in candidates is already bigger than target, so no combination works. The answer is an empty list.
Example 4
candidates = [7,2,3]target = 7return = [[2,2,3],[7]]Here candidates comes in a different order, but the output order does not depend on it. The answer is the same two combinations as in the first example, sorted the same way.
Constraints
1 <= candidates.length <= 8- You may assume all candidate values are distinct, and
1 <= candidates[i] <= 40. 1 <= target <= 30
More IMC Trading Phone Screen questions
- Election District Budget Allocation AnalysisPHONE SCREEN · Seen Aug 2026
- Uniform Variables: Smaller Below Larger SquaredPHONE SCREEN · Seen Mar 2026
- Valid Range of a Three-Outcome Probability ParameterPHONE SCREEN · Seen Dec 2025
- Expected Fishing CatchPHONE SCREEN · Seen Dec 2025
- Count Routes Through a City GridPHONE SCREEN · Seen Nov 2024
- Expected City Arrival and Return DaysPHONE SCREEN · Seen Nov 2024
- Stationary City Population SharesPHONE SCREEN · Seen Nov 2024
- Maximum Falling Path SumPHONE SCREEN · Seen Nov 2024
- Trading Profit Percentage ChangePHONE SCREEN · Seen Feb 2024
- Poker Suit Expected ProfitPHONE SCREEN · Seen Sep 2023
Similar questions at other companies
- Largest Number Below N from Reusable DigitsByteDance · PHONE SCREENByteDance interview questions
- SubsetsTemu · PHONE SCREENTemu interview questions
- PermutationsEPAM · PHONE SCREENEPAM interview questions
- Expand Attribute CombinationsPinterest · PHONE SCREENPinterest interview questions
- All Subsets with a Given SumApple · PHONE SCREENApple interview questions
- SubsetsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Validate a Mahjong Hand PartitionDuolingo · PHONE SCREENDuolingo interview questions
- Winning Draws for a Mahjong HandZip · PHONE SCREENZip interview questions
- Count Plus and Minus Sign PatternsAdobe · PHONE SCREENAdobe interview questions
- Generate Parameter CombinationsGecko Robotics · OA · PHONE SCREEN