Skip to content

Commit f2d6f3a

Browse files
authored
Create 1894. Find the Student that Will Replace the Chalk (#573)
2 parents 6c3a20a + 46c432e commit f2d6f3a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
int chalkReplacer(vector<int>& chalk, int k)
4+
{
5+
long allSum = 0;
6+
7+
// Calculate the total sum of all elements in the chalk array
8+
for(auto val : chalk)
9+
allSum += val;
10+
11+
// Calculate the remainder of k when divided by allSum
12+
// This determines how much chalk remains after several full cycles
13+
long mod = k % allSum, n = chalk.size();
14+
15+
// Iterate through the chalk array
16+
for(int i = 0; i < n; i++)
17+
{
18+
// If the current student's chalk usage is more than the remaining chalk, return their index
19+
if(chalk[i] > mod) return i;
20+
21+
// Otherwise, subtract the current student's chalk usage from the remaining chalk
22+
mod -= chalk[i];
23+
}
24+
25+
// This line should never be reached since the problem guarantees a solution.
26+
return mod;
27+
}
28+
};
29+

0 commit comments

Comments
 (0)