Skip to main content
Back to problems
#1894
Medium Algorithms

Find the student that will replace the chalk

Array Binary Search Simulation Prefix Sum
53.2% acceptance
Feb 25, 2026
1211
134
There are n students in a class, numbered 0..n-1. Student i uses chalk[i] pieces per round. Given k pieces of chalk, return which student runs out first.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn chalk_replacer(chalk: Vec<i32>, k: i32) -> i32 {
    let total: i64 = chalk.iter().map(|&x| x as i64).sum();
    let mut remaining = k as i64 % total;
    for (i, &c) in chalk.iter().enumerate() {
      if remaining < c as i64 {
        return i as i32;
      }
      remaining -= c as i64;
    }
    0
  }
}