Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Generating desired combinations in JavaScript
In JavaScript, generating combinations that sum to a target value is a common programming challenge. This problem involves finding all unique combinations of integers from 1 to 9 with a specific length that sum to a target value.
Understanding the Problem
We need to generate combinations where:
- m represents the size of each combination
- n represents the target sum
- Each combination uses unique numbers from 1 to 9
For example, if m = 3 and n = 9, valid combinations include [1, 2, 6], [1, 3, 5], and [2, 3, 4]. Each has exactly 3 numbers that sum to 9.
Algorithm Approach
We use a recursive backtracking approach:
- Start with the smallest available number (1)
- For each number, decide whether to include it or skip it
- If included, reduce both m (remaining slots) and n (remaining sum)
- Continue until m = 0 and n = 0 (valid combination found)
- Backtrack if constraints are violated
Implementation
const m = 3, n = 9;
// Function for finding all possible combinations
const findSum = (m, n) => {
const res = [];
const search = (from, prefix, m, n) => {
// Base case: found valid combination
if (m === 0 && n === 0) {
res.push(prefix);
return;
}
// No more numbers available or impossible to reach target
if (from > 9 || m === 0 || n <= 0) return;
// Include current number
search(from + 1, prefix.concat(from), m - 1, n - from);
// Exclude current number
search(from + 1, prefix, m, n);
};
search(1, [], m, n);
return res;
};
console.log("Combinations of size", m, "that sum to", n, ":");
console.log(findSum(m, n));
Combinations of size 3 that sum to 9 : [ [ 1, 2, 6 ], [ 1, 3, 5 ], [ 2, 3, 4 ] ]
How It Works
The algorithm explores two choices for each number:
- Include: Add the number to the current combination and reduce remaining slots (m-1) and sum (n-current)
- Exclude: Skip the number and move to the next
The recursion terminates when we find a valid combination (m=0, n=0) or when constraints make success impossible.
Alternative Example
// Finding combinations of size 2 that sum to 10
const result = findSum(2, 10);
console.log("Size 2, sum 10:", result);
// Finding combinations of size 4 that sum to 15
const result2 = findSum(4, 15);
console.log("Size 4, sum 15:", result2);
Size 2, sum 10: [ [ 1, 9 ], [ 2, 8 ], [ 3, 7 ], [ 4, 6 ] ] Size 4, sum 15: [ [ 1, 2, 3, 9 ], [ 1, 2, 4, 8 ], [ 1, 2, 5, 7 ], [ 1, 3, 4, 7 ], [ 1, 3, 5, 6 ], [ 2, 3, 4, 6 ] ]
Complexity Analysis
Time Complexity: O(2^9) in the worst case, as we make binary decisions for each number from 1 to 9. However, pruning reduces actual runtime significantly.
Space Complexity: O(k × m) where k is the number of valid combinations and m is the combination size. Additional O(m) for recursion stack depth.
Conclusion
This backtracking solution efficiently generates all valid combinations using recursive exploration. While the theoretical complexity is exponential, practical performance is good due to early termination and constraint pruning.
