-
Notifications
You must be signed in to change notification settings - Fork 19.4k
/
BinomialCoefficient.java
40 lines (34 loc) · 1.24 KB
/
BinomialCoefficient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.thealgorithms.maths;
/*
* Java program for Binomial Cofficients
* Binomial Cofficients: A binomial cofficient C(n,k) gives number ways
* in which k objects can be chosen from n objects.
* Wikipedia: https://en.wikipedia.org/wiki/Binomial_coefficient
*
* Author: Akshay Dubey (https://github.com/itsAkshayDubey)
*
* */
public final class BinomialCoefficient {
private BinomialCoefficient() {
}
/**
* This method returns the number of ways in which k objects can be chosen from n objects
*
* @param totalObjects Total number of objects
* @param numberOfObjects Number of objects to be chosen from total_objects
* @return number of ways in which no_of_objects objects can be chosen from total_objects
* objects
*/
public static int binomialCoefficient(int totalObjects, int numberOfObjects) {
// Base Case
if (numberOfObjects > totalObjects) {
return 0;
}
// Base Case
if (numberOfObjects == 0 || numberOfObjects == totalObjects) {
return 1;
}
// Recursive Call
return (binomialCoefficient(totalObjects - 1, numberOfObjects - 1) + binomialCoefficient(totalObjects - 1, numberOfObjects));
}
}