Find the Student that Will Replace the Chalk

There are `n` students numbered 0 to n - 1 who take turns solving problems in order, starting again from student 0 after student n - 1. Student `i` uses `chalk[i]` pieces of chalk per problem. You start with `k` pieces. When the chalk left is strictly less than what the current student needs, that student must replace the chalk. Return the index of that student.

30:00

Find the Student that Will Replace the Chalk
medium
Free written solution
Topics
Companies

There are n students numbered 0 to n - 1 who take turns solving problems in order, starting again from student 0 after student n - 1. Student i uses chalk[i] pieces of chalk per problem. You start with k pieces. When the chalk left is strictly less than what the current student needs, that student must replace the chalk. Return the index of that student.

Example 1:
Input: {"chalk":[5,1,5],"k":22}
Output: 0
Constraints:
  • chalk.length=n\text{chalk.length} = n

  • 1≤n≤1051 \leq n \leq 10^5

  • 1≤chalk[i]≤1051 \leq chalk[i] \leq 10^5

  • 1≤k≤1091 \leq k \leq 10^9

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"chalk":[5,1,5],"k":22}
k = 22 pieces
one round = ?
0

5

1

1

2

5

one round of chalk, split by student (running totals below)

0

5

0

0

Student 0 uses 5 piece(s) per problem, so one turn of student 0 costs 5.

Variables
VariableValue
k22
total5
DepthFunction Call
Stack empty
0/9