Combinations (n choose k)

EasyProbabilityMathSorting

Description

Given non-negative integers n and k, return the number of ways to choose k items from n without regard to order, the binomial coefficient C(n, k) = n! / (k! (n - k)!). Return 0 when k > n.

Examples

Input:5, 2
Output:10
Explanation:

Choosing k items from n while ignoring order counts each unordered group once, which is exactly the binomial coefficient.

Input:5, 0
Output:1
Explanation:

Choosing k items from n while ignoring order counts each unordered group once, which is exactly the binomial coefficient.

Input:6, 3
Output:20
Explanation:

Choosing k items from n while ignoring order counts each unordered group once, which is exactly the binomial coefficient.

Constraints

  • 0 ≤ k, n ≤ 30

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.