Code RoomSurjective function count
HardPrep Room Coding #1287

Surjective function count

CodingAlgorithms & data structuresSenior–Staff~35 min

Count the number of surjective (onto) functions from a set of n distinct elements to a set of k distinct labels — i.e., colorings of n items with exactly k colors where every color is used at least once. Return the count as an exact integer (Python big ints). Constraints: 0 <= n <= 200, 0 <= k <= 200. Use inclusion-exclusion: sum over i of (-1)^i * C(k,i) * (k-i)^n.

Implement
count_surjections(n: int, k: int) → int
Examples
in[3,2]out6
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 35 min
InputExpectedGot
[3,2]6not run yetsample