
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Combination sum problem using JavaScript
Suppose we are given a set of candidate numbers (without duplicates) and a target number (target).
We are required to write a function that finds all unique combinations in candidates where the candidate numbers sum to the target.
The same repeated number may be chosen from candidates an unlimited number of times.
Note −
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
For example −
If the inputs are −
candidates = [2,3,6,7], target = 7,
The solution to this can be −
[ [7], [2,2,3] ];
Since the problem is to get all the possible results, not the best or the number of results, thus we don’t need to consider Dynamic Programming, backtracking approach using recursion is needed to handle it.
Example
Following is the code −
const recursiveSum = ( candidates, remainingSum, finalCombinations = [], currentCombination = [], startFrom = 0, ) => { if (remainingSum < 0) { return finalCombinations; } if (remainingSum === 0) { finalCombinations.push(currentCombination.slice()); return finalCombinations; } for (let candidateIndex = startFrom; candidateIndex < candidates.length; candidateIndex += 1) { const currentCandidate = candidates[candidateIndex]; currentCombination.push(currentCandidate); recursiveSum( candidates, remainingSum - currentCandidate, finalCombinations, currentCombination, candidateIndex, ); currentCombination.pop(); } return finalCombinations; } const combinationSum = (candidates, target) => recursiveSum(candidates, target); console.log(combinationSum([2, 3, 6, 7], 7));
Output
Following is the output on console −
[ [ 2, 2, 3 ], [ 7 ] ]
- Related Articles
- Two sum problem in linear time in JavaScript
- Subset Sum Problem
- Combination Sum in Python
- Combination Sum II in C++
- Combination Sum IIII in C++
- Combination Sum IV in C++
- How to find the unique combination k sum that corresponds to k sum using C#?
- Python Program for Subset Sum Problem
- Substring combination in JavaScript
- Problem Can we fit remaining passengers in the bus using JavaScript
- Snail Trail Problem in JavaScript
- Recursive Staircase problem in JavaScript
- Distributing Bananas Problem in JavaScript
- Recursion problem Snail Trail in JavaScript
- 2 Key keyboard problem in JavaScript
