Find the Student that Will Replace the Chalk

We can find the student that will replace the chalk pieces by following these steps:

  1. Calculate the total sum of chalk required for one round of problems for all students.
  2. Find the remaining chalk after multiple full rounds using the modulo operator.
  3. Iterate through the students and deduct the chalk required by each student. When the remaining chalk is less than the chalk required by a student, return that student’s index.

Here’s the code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution:
    def chalkReplacer(self, chalk: List[int], k: int) -> int:
        # Find the total sum of chalk required for one round
        total_chalk = sum(chalk)

        # Find the remaining chalk after multiple full rounds
        k %= total_chalk

        # Iterate through the students and find the student that will replace the chalk
        for i, chalk_needed in enumerate(chalk):
            if k < chalk_needed:
                return i
            k -= chalk_needed

This code first finds the total chalk needed for one full round. Then it finds the remaining chalk after multiple full rounds by using the modulo operator. Finally, it iterates through the students, deducting the chalk needed by each student until the remaining chalk is less than the chalk needed by a student. It then returns that student’s index.