-
Notifications
You must be signed in to change notification settings - Fork 0
/
N Choose K Count3.java
62 lines (38 loc) · 1.21 KB
/
N Choose K Count3.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// N Choose K Count
// Given a n and k, calculate the number of possible n choose k combinations (without repetition).
// Examples
// Input Output
// 3
// 2
// 3
// 49
// 6
// 13983816
// 1. Get the input n and k
// 2. Create a method to calculate the factorial of a number
// 3. Calculate the result using the formula: n! / (k! * (n - k)!)
// 4. Print the result
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = Integer.parseInt(scanner.nextLine());
int k = Integer.parseInt(scanner.nextLine());
long result = binomialCoefficient(n, k);
System.out.println(result);
}
private static long factorial(int n) {
if (n < 0) return -1; // handle negative input
if (n == 0) return 1; // handle 0 input
long result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
private static long binomialCoefficient(int n, int k) {
if (n < k) return -1; // handle k > n
long result = factorial(n) / (factorial(k) * factorial(n - k));
return result;
}
}