Skip to content

Commit cc86fef

Browse files
authored
Create Find the Student that Will Replace the Chalk.java
1 parent 0e27969 commit cc86fef

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int chalkReplacer(int[] chalk, int k) {
3+
int initialResult = chalkReplacerHelper(chalk, k);
4+
return initialResult != -1 ? initialResult
5+
: chalkReplacerHelper(chalk, k % Arrays.stream(chalk).sum());
6+
}
7+
8+
private int chalkReplacerHelper(int[] chalk, int k) {
9+
for (int i = 0; i < chalk.length; i++) {
10+
if (k - chalk[i] < 0) {
11+
return i;
12+
}
13+
k -= chalk[i];
14+
}
15+
return -1;
16+
}
17+
}

0 commit comments

Comments
 (0)