Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c9104b8

Browse files
authoredMar 14, 2025··
2226_Maximum_Candies_Allocated_to_K_Childrens.java
1 parent d6f4223 commit c9104b8

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
 
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Problem Number: 2226
2+
3+
// Maximum Candies allocated to K Childrens.
4+
5+
class Solution {
6+
public int maximumCandies(int[] candies, long k) {
7+
int l = 1;
8+
int r = (int) (Arrays.stream(candies).asLongStream().sum() / k);
9+
10+
while (l < r) {
11+
final int m = (l + r) / 2;
12+
if (numChildren(candies, m) < k)
13+
r = m;
14+
else
15+
l = m + 1;
16+
}
17+
18+
return numChildren(candies, l) >= k ? l : l - 1;
19+
}
20+
21+
private long numChildren(int[] candies, int m) {
22+
return Arrays.stream(candies).asLongStream().reduce(0L, (subtotal, c) -> subtotal + c / m);
23+
}
24+
}

0 commit comments

Comments
 (0)
Please sign in to comment.