new class js -advanced
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
function combinations(n, k) {
|
||||
// Base Cases
|
||||
if (k === 0 || k === n) {
|
||||
return 1;
|
||||
}
|
||||
// If k is greater than n, there are no combinations
|
||||
if (k > n) {
|
||||
return 0;
|
||||
}
|
||||
// Recursive Cases
|
||||
return combinations(n - 1, k - 1) + combinations(n - 1, k);
|
||||
}
|
||||
|
||||
// Test Cases
|
||||
console.log(combinations(5, 2)); // => 10
|
||||
console.log(combinations(6, 3)); // => 20
|
||||
console.log(combinations(4, 0)); // => 1
|
||||
console.log(combinations(4, 4)); // => 1
|
||||
console.log(combinations(5, 6)); // => 0
|
||||
Reference in New Issue
Block a user