2023-10-30 11:10:02 +05:30
|
|
|
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
|
2021-10-06 00:13:25 +05:30
|
|
|
}
|
2023-10-30 11:10:02 +05:30
|
|
|
if (currentValue > n) {
|
|
|
|
|
// Check for exceeding the range
|
|
|
|
|
return
|
2021-10-06 00:13:25 +05:30
|
|
|
}
|
2023-10-30 11:10:02 +05:30
|
|
|
currentCombination.push(currentValue++)
|
|
|
|
|
findCombinations()
|
|
|
|
|
currentCombination.pop()
|
|
|
|
|
findCombinations()
|
|
|
|
|
currentValue--
|
2021-10-06 14:03:27 +05:30
|
|
|
}
|
2023-10-30 11:10:02 +05:30
|
|
|
|
|
|
|
|
findCombinations()
|
|
|
|
|
|
|
|
|
|
return allCombinations
|
2021-10-06 00:13:25 +05:30
|
|
|
}
|
|
|
|
|
|
2023-10-30 11:10:02 +05:30
|
|
|
export { generateCombinations }
|