This question evaluates proficiency in randomized algorithms and probability-based sampling, along with algorithmic preprocessing and data-structure design for efficient repeated sampling.
You are given a discrete probability distribution for an -sided die as an array p[0..M-1], where each p[i] is non-negative.
Design a function sample(p) -> int that returns a random index i such that:
p
.
What if the probabilities do not sum to 1? Explain how your approach handles this.