Combination Sum
Write a function that returns all unique combinations of candidates where the chosen numbers sum to the target.
The same number may be chosen from candidates an unlimited number of times. Return the combinations in any order.
Input: An array of distinct integers candidates and an integer target.
Output: A 2D array containing all unique combinations of numbers that sum to the target.
Example Inputs & Outputs
// Example 1: Input: candidates = [2,3,6,7], target = 7 Output: [[2,2,3],[7]] // Example 2: Input: candidates = [2,3,5], target = 8 Output: [[2,2,2,2],[2,3,3],[3,5]] // Example 3: Input: candidates = [2], target = 1 Output: [] // Example 4: Input: candidates = [1], target = 2 Output: [[1,1]] // Example 5: Input: candidates = [1], target = 1 Output: [[1]]
Constraints & Edge Cases
- All numbers in candidates are positive and distinct.
- Candidates can be reused multiple times.
- No duplicate combinations allowed (i.e., same frequency counts considered same).
- Return empty array if no combination is possible.
Companies:
Solve Similar questions 🔥
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
