Find the Student that Will Replace the Chalk (LeetCode 1894): Solution and Explanation

Medium
Array
Binary Search
Simulation
Prefix Sum

Students take turns in a loop, and student i uses chalk[i] pieces per turn. Starting with k pieces, return the index of the first student who needs more chalk than is left. For chalk = [5,1,5] and k = 22 the answer is 0.

Intuition

Simulating turn by turn can take up to k / min(chalk) steps, which is far too many when k is 10^9 and every student uses 1 piece.

A full round of the class always uses the same amount, total = sum(chalk). Removing whole rounds never changes who runs out, so only k % total pieces matter, and that is less than one round.

Inside a single round the chalk used so far only grows, so the prefix sums are sorted. The student who runs out is the first index whose prefix sum is greater than the remainder, which is a classic binary search.

Approach: Remainder plus binary search on prefix sums

  1. Build prefix[i] = chalk[0] + ... + chalk[i] with a 64-bit running total. The last entry is total.
  2. Replace k with k % total, the chalk left after all complete rounds.
  3. Binary search with lo = 0 and hi = n - 1. If prefix[mid] > k, student mid cannot finish, so set hi = mid. Otherwise everyone up to mid finishes, so set lo = mid + 1.
  4. When lo == hi, return lo.

Step-by-step example

chalk = [5,1,5], k = 22
  1. Prefix sums are [5, 6, 11], so one round costs 11.
  2. 22 % 11 = 0: two full rounds use all 22 pieces and 0 are left.
  3. lo = 0, hi = 2. mid = 1: prefix[1] = 6 > 0, so hi = 1.
  4. mid = 0: prefix[0] = 5 > 0, so hi = 0.
  5. lo = hi = 0. Student 0 needs 5 pieces with 0 left, so the answer is 0.

Code: Python, Java, C++

1class Solution:
2    def chalkReplacer(self, chalk: List[int], k: int) -> int:
3        prefix = []
4        total = 0
5        for c in chalk:
6            total += c
7            prefix.append(total)
8        k %= total
9        lo, hi = 0, len(chalk) - 1
10        while lo < hi:
11            mid = (lo + hi) // 2
12            if prefix[mid] > k:
13                hi = mid
14            else:
15                lo = mid + 1
16        return lo
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Building the prefix sums is O(n). The binary search adds O(log n).

Space

O(n)

The prefix array holds n running totals. A second linear pass instead of the search would bring it to O(1).

Other approaches

Remainder plus linear scan (time O(n), space O(1))

After k %= total, walk the students and subtract chalk[i] until it is larger than what is left. Same O(n) time and O(1) extra space.

Direct simulation (time O(k / min(chalk)), space O(1))

Loop over the class subtracting chalk until someone cannot be served. It can run about 10^9 iterations when every student uses 1 piece.

Common mistakes

  • Summing in 32-bit. With 10^5 students using up to 10^5 pieces each, the total reaches 10^10 and overflows a 32-bit int.
  • Using >= instead of >. A student whose need exactly equals the chalk left can still finish, so the search looks for the first prefix strictly greater than the remainder.
  • Skipping the modulo and binary searching on k itself. When k is larger than one round, no prefix exceeds it and the search returns the wrong student.

Frequently asked questions

Why is it safe to throw away full rounds?

Each full round uses exactly total pieces and ends back at student 0 with nobody running out, because k was at least total before the round. So the situation after removing a round is the same problem with a smaller k.

Why are the prefix sums sorted?

Every chalk[i] is at least 1, so each running total is strictly larger than the one before. That monotonic order is what makes binary search valid.

Can the remainder be 0?

Yes. Then nobody can afford a turn and student 0 replaces the chalk. The search handles it because prefix[0] is at least 1, which is greater than 0.