MCPcopy Index your code
hub / github.com/TheAlgorithms/JavaScript / generateCombinations

Function generateCombinations

Backtracking/AllCombinationsOfSizeK.js:1–26  ·  view source on GitHub ↗
(n, k)

Source from the content-addressed store, hash-verified

1function generateCombinations(n, k) {
2 let currentCombination = []
3 let allCombinations = [] // will be used for storing all combinations
4 let currentValue = 1
5
6 function findCombinations() {
7 if (currentCombination.length === k) {
8 // Add the array of size k to the allCombinations array
9 allCombinations.push([...currentCombination])
10 return
11 }
12 if (currentValue > n) {
13 // Check for exceeding the range
14 return
15 }
16 currentCombination.push(currentValue++)
17 findCombinations()
18 currentCombination.pop()
19 findCombinations()
20 currentValue--
21 }
22
23 findCombinations()
24
25 return allCombinations
26}
27
28export { generateCombinations }

Callers 1

Calls 1

findCombinationsFunction · 0.85

Tested by

no test coverage detected