Find the Student that Will Replace the Chalk

MediumArrayBinary SearchSimulationPrefix Sum

Solution

export function chalkReplacer(chalk: number[], k: number): number {
  const n = chalk.length;
  const totalChalk = chalk.reduce((acc, c) => acc + c, 0);
 
  let remainChalk = k % totalChalk;
  for (let i = 0; i < n; i++) {
    if (remainChalk < chalk[i]) {
      return i;
    }
    remainChalk -= chalk[i];
  }
  return 0;
}

Complexity

  • Time: O(N)
  • Space: O(1)