Find the Student that Will Replace the Chalk

There are n students in a class numbered from 0 to n - 1. The teacher will give each student a problem starting with student number 0, then student number 1, and so on until the teacher reaches student number n - 1. After that, the teacher will restart the process, starting with student number 0 again.

You are given a 0-indexed integer array chalk and an integer k. There are initially k pieces of chalk. When student number i is given a problem to solve, they will use chalk[i] pieces of chalk to solve that problem. However, if the current number of chalk pieces is strictly less than chalk[i], then student number i will be asked to replace the chalk.

Return the index of the student that will replace the chalk pieces.

Example 1
Inputchalk = [5,1,5], k = 22
Output0
After the students use chalk in order until k becomes 0, student number 0 does not have enough chalk and must replace it.
Example 2
Inputchalk = [3,4,1,2], k = 25
Output1
After the listed turns leave only 2 pieces of chalk, student number 1 needs 4 pieces and must replace it.

Constraints

  • chalk.length == n
  • 1 <= n <= 10^5
  • 1 <= chalk[i] <= 10^5
  • 1 <= k <= 10^9

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate