Files
JavaScript/Backtracking/AllCombinationsOfSizeK.js
Rahul Bhandari cafb3433e8 Update AllCombinationsOfSizeK.js (#1530)
* Update AllCombinationsOfSizeK.js

* Update AllCombinationsOfSizeK.js

* Update AllCombinationsOfSizeK.test.js

Changes made it the type of testing. 
Instead of testing the class now the program will test the function

* Update AllCombinationsOfSizeK.js

* Update AllCombinationsOfSizeK.js

* Update AllCombinationsOfSizeK.js

* Update AllCombinationsOfSizeK.test.js

* Update AllCombinationsOfSizeK.test.js
2023-10-30 11:10:02 +05:30

29 lines
683 B
JavaScript

function generateCombinations(n, k) {
let currentCombination = []
let allCombinations = [] // will be used for storing all combinations
let currentValue = 1
function findCombinations() {
if (currentCombination.length === k) {
// Add the array of size k to the allCombinations array
allCombinations.push([...currentCombination])
return
}
if (currentValue > n) {
// Check for exceeding the range
return
}
currentCombination.push(currentValue++)
findCombinations()
currentCombination.pop()
findCombinations()
currentValue--
}
findCombinations()
return allCombinations
}
export { generateCombinations }