Combination Sum
Updated
Problem statement
candidates is an array of distinct integers, and target is an integer. Find all the ways to choose numbers from candidates so that the chosen numbers add up to target. Any number can be chosen more than once.
Only how many times each number is chosen matters, not the order of the choices: [2,2,3] and [3,2,2] are one combination, and it must appear only once.
Return the combinations in one fixed order. Inside each combination, put the numbers in non-decreasing order. Then list the combinations in lexicographic order, comparing numbers as integers: for example, [2,2,6] comes before [2,3,5] because 2 < 3 at the second position. If no combination adds up to target, return an empty list.
Function
combinationSum(candidates: int[], target: int) → List<List<Integer>>Examples
Example 1
candidates = [6,2,5]target = 10return = [[2,2,2,2,2],[2,2,6],[5,5]]Three combinations add up to 10: 2 + 2 + 2 + 2 + 2, 2 + 2 + 6 and 5 + 5. Each one is written smallest number first, and they are listed in lexicographic order. No combination with 5 and 2 works: one 5 plus any number of 2s is odd, and two 5s already make 10, so there is no room for a 2.
Example 2
candidates = [10,4]target = 7return = []10 is larger than 7, and 7 is not a multiple of 4, so nothing adds up to 7 and the answer is an empty list.
Example 3
candidates = [12,3,8]target = 12return = [[3,3,3,3],[12]]3 + 3 + 3 + 3 and 12 both add up to 12. The numbers are compared as integers, so [3,3,3,3] comes before [12] because 3 < 12. 8 is in no combination: 12 - 8 = 4, and 4 cannot be made from 3s.
Constraints
1 <= candidates.length <= 302 <= candidates[i] <= 40- All numbers in
candidatesare distinct. 1 <= target <= 40- For every input, fewer than
150combinations add up totarget.
More Anduril interview questions
- Pacific Atlantic Water FlowPHONE SCREEN · Seen Aug 2026
- Choose the Front Football TeamPHONE SCREEN · Seen May 2026
- Maximum Visible DronesPHONE SCREEN · Seen Jun 2024
- Build a Sorted Circular Singly Linked ListPHONE SCREEN · Seen Feb 2024
- Sparse Vector Dot ProductPHONE SCREEN · Seen Apr 2021
- Add Word and Suffix SearchONSITE INTERVIEW · Seen Aug 2026
- Binary Tree Preorder TraversalONSITE INTERVIEW · Seen Aug 2026
- Operator-First Array CalculatorONSITE INTERVIEW · Seen Aug 2026
- Daily TemperaturesONSITE INTERVIEW · Seen Jun 2024
- Highlighted Context WindowsONSITE INTERVIEW · Seen Jun 2024
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
- Combination Sum with Unlimited ReuseIMC Trading · PHONE SCREENIMC Trading 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