Calculate nCr
Given separate integers n and r, return the number of ways to choose r elements from n distinct elements when order does not matter. If r > n, return 0.
Given separate integers n and r, return the number of ways to choose r elements from n distinct elements when order does not matter. If r > n, return 0.
0 <= n <= 33 and 0 <= r <= 33. The maximum result is 33C16 = 1,166,803,110. The multiply-before-divide recurrence can exceed signed 32-bit range even though its result fits, so fixed-width implementations need a signed 64-bit intermediate.
Example 1: input n = 5, r = 2; output 10. There are ten distinct two-element subsets.
Example 2: input n = 3, r = 5; output 0. A three-element set cannot provide five elements.
Example 3: input n = 0, r = 0; output 1. The empty set has one subset: the empty subset.
Category: Math & Numbers. Topic: math. Difficulty: Easy. Patterns: math.