Combinations C(n, k) & Permutations P(n, k)

Basics
BasicsCombinatoricsGoogleMetaJane Street

Problem

Implement comb_and_perm(n, k) returning (C(n, k), P(n, k)) as exact Python integers, where C(n,k)=(nk)=n!k!(n−k)!C(n, k) = \binom{n}{k} = \frac{n!}{k!(n-k)!} (order does not matter) and P(n,k)=n!(n−k)!P(n, k) = \frac{n!}{(n-k)!} (order matters).

Why It Matters: Counting combinations vs. permutations is the foundation of discrete probability, binomial coefficients, and combinatorial interview questions.

Function Signature

import math

def comb_and_perm(n: int, k: int) -> tuple[int, int]:

Sample Input & Output

Example 1: Choosing 2 From 5 Items

Input:

n = 5, k = 2

Expected Output:

(10, 20)

Explanation: C(5, 2) = 10 unordered pairs; P(5, 2) = 5 * 4 = 20 ordered pairs.

Example 2: Boundary Case k = 0

Input:

n = 6, k = 0

Expected Output:

(1, 1)

Explanation: There is exactly 1 way to choose 0 items whether order matters or not: (1, 1).

Constraints & Edge Cases

  • Return (combinations, permutations) as Python ints.
  • Use Python's exact integer math.comb(n, k) and math.perm(n, k) (avoids float overflow).
  • Guaranteed 0 <= k <= n.

Constraints & Evaluation Criteria

  • Return `(combinations, permutations)` as Python ints.

Hints

Python 3
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
n=5, k=2
Expected Output / Criteria
(10, 20)