Find the Student that Will Replace the Chalk
We can find the student that will replace the chalk pieces by following these steps:
- Calculate the total sum of chalk required for one round of problems for all students.
- Find the remaining chalk after multiple full rounds using the modulo operator.
- 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:
|
|
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.