|
| 1 | +--- |
| 2 | +id: Maximum-Candies-Allocated-to-K-Children |
| 3 | +title: Maximum Candies Allocated to K Children |
| 4 | +sidebar_label: 2226-Maximum Candies Allocated to K Children |
| 5 | +tags: |
| 6 | + - Arrays |
| 7 | + - C++ |
| 8 | + - Java |
| 9 | + - Python |
| 10 | +description: "This document provides solutions to this problem implemented in C++, Java, and Python." |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem |
| 14 | + |
| 15 | +You are given a 0-indexed integer array candies. Each element in the array denotes a pile of candies of size candies[i]. You can divide each pile into any number of sub piles, but you cannot merge two piles together. |
| 16 | + |
| 17 | +You are also given an integer k. You should allocate piles of candies to k children such that each child gets the same number of candies. Each child can take at most one pile of candies and some piles of candies may go unused. |
| 18 | + |
| 19 | +Return the maximum number of candies each child can get. |
| 20 | +### Examples |
| 21 | + |
| 22 | +**Example 1:** |
| 23 | + |
| 24 | +Input: candies = [5,8,6], k = 3 |
| 25 | +Output: 5 |
| 26 | +Explanation: We can divide candies[1] into 2 piles of size 5 and 3, and candies[2] into 2 piles of size 5 and 1. We now have five piles of candies of sizes 5, 5, 3, 5, and 1. We can allocate the 3 piles of size 5 to 3 children. It can be proven that each child cannot receive more than 5 candies. |
| 27 | + |
| 28 | +**Example 2:** |
| 29 | + |
| 30 | +Input: candies = [2,5], k = 11 |
| 31 | +Output: 0 |
| 32 | +Explanation: There are 11 children but only 7 candies in total, so it is impossible to ensure each child receives at least one candy. Thus, each child gets no candy and the answer is 0. |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +### Constraints |
| 38 | + |
| 39 | +- `1 <= candies.length <= 105` |
| 40 | +- `1 <= candies[i] <= 107` |
| 41 | +- `1 <= k <= 1012` |
| 42 | + |
| 43 | +### Approach |
| 44 | + |
| 45 | +Initialize: Set the search range from 1 to the maximum number of candies in any pile. |
| 46 | +Binary Search: |
| 47 | +- Calculate the middle value of the current range. |
| 48 | +- Check if it's possible to distribute this many candies per child to all k children by dividing the piles. |
| 49 | +- Adjust the range based on whether the distribution was possible. |
| 50 | +Return Result: The highest feasible value found during the search is the answer. |
| 51 | + |
| 52 | +### Solution |
| 53 | + |
| 54 | +#### Code in Different Languages |
| 55 | + |
| 56 | +### C++ Solution |
| 57 | + |
| 58 | +```cpp |
| 59 | +#include <vector> |
| 60 | +#include <algorithm> |
| 61 | +using namespace std; |
| 62 | + |
| 63 | +bool canDistribute(const vector<int>& candies, long long mid, long long k) { |
| 64 | + long long count = 0; |
| 65 | + for (int candy : candies) { |
| 66 | + count += candy / mid; |
| 67 | + } |
| 68 | + return count >= k; |
| 69 | +} |
| 70 | + |
| 71 | +int maxCandies(vector<int>& candies, long long k) { |
| 72 | + long long left = 1, right = *max_element(candies.begin(), candies.end()); |
| 73 | + while (left <= right) { |
| 74 | + long long mid = left + (right - left) / 2; |
| 75 | + if (canDistribute(candies, mid, k)) { |
| 76 | + left = mid + 1; |
| 77 | + } else { |
| 78 | + right = mid - 1; |
| 79 | + } |
| 80 | + } |
| 81 | + return right; |
| 82 | +} |
| 83 | + |
| 84 | +// Example usage: |
| 85 | +#include <iostream> |
| 86 | +int main() { |
| 87 | + vector<int> candies1 = {5, 8, 6}; |
| 88 | + long long k1 = 3; |
| 89 | + cout << maxCandies(candies1, k1) << endl; // Output: 5 |
| 90 | + |
| 91 | + vector<int> candies2 = {2, 5}; |
| 92 | + long long k2 = 11; |
| 93 | + cout << maxCandies(candies2, k2) << endl; // Output: 0 |
| 94 | + |
| 95 | + return 0; |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +``` |
| 101 | +
|
| 102 | +### Java Solution |
| 103 | +
|
| 104 | +```java |
| 105 | +public class MaxCandies { |
| 106 | + public static boolean canDistribute(int[] candies, long mid, long k) { |
| 107 | + long count = 0; |
| 108 | + for (int candy : candies) { |
| 109 | + count += candy / mid; |
| 110 | + } |
| 111 | + return count >= k; |
| 112 | + } |
| 113 | +
|
| 114 | + public static int maxCandies(int[] candies, long k) { |
| 115 | + long left = 1, right = Integer.MIN_VALUE; |
| 116 | + for (int candy : candies) { |
| 117 | + right = Math.max(right, candy); |
| 118 | + } |
| 119 | + |
| 120 | + while (left <= right) { |
| 121 | + long mid = left + (right - left) / 2; |
| 122 | + if (canDistribute(candies, mid, k)) { |
| 123 | + left = mid + 1; |
| 124 | + } else { |
| 125 | + right = mid - 1; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return (int) right; |
| 130 | + } |
| 131 | +
|
| 132 | + public static void main(String[] args) { |
| 133 | + int[] candies1 = {5, 8, 6}; |
| 134 | + long k1 = 3; |
| 135 | + System.out.println(maxCandies(candies1, k1)); // Output: 5 |
| 136 | +
|
| 137 | + int[] candies2 = {2, 5}; |
| 138 | + long k2 = 11; |
| 139 | + System.out.println(maxCandies(candies2, k2)); // Output: 0 |
| 140 | + } |
| 141 | +} |
| 142 | +
|
| 143 | +
|
| 144 | +``` |
| 145 | + |
| 146 | +### Python Solution |
| 147 | + |
| 148 | +```python |
| 149 | +def maxCandies(candies, k): |
| 150 | + def canDistribute(mid): |
| 151 | + count = 0 |
| 152 | + for candy in candies: |
| 153 | + count += candy // mid |
| 154 | + return count >= k |
| 155 | + |
| 156 | + left, right = 1, max(candies) |
| 157 | + while left <= right: |
| 158 | + mid = (left + right) // 2 |
| 159 | + if canDistribute(mid): |
| 160 | + left = mid + 1 |
| 161 | + else: |
| 162 | + right = mid - 1 |
| 163 | + |
| 164 | + return right |
| 165 | + |
| 166 | +# Example usage: |
| 167 | +candies1 = [5, 8, 6] |
| 168 | +k1 = 3 |
| 169 | +print(maxCandies(candies1, k1)) # Output: 5 |
| 170 | + |
| 171 | +candies2 = [2, 5] |
| 172 | +k2 = 11 |
| 173 | +print(maxCandies(candies2, k2)) # Output: 0 |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | +``` |
| 178 | + |
| 179 | +### Complexity Analysis |
| 180 | + |
| 181 | +### Time Complexity: $O(n*logm)$ |
| 182 | + |
| 183 | +> **Reason**:Binary search runs in O(logm), and each check inside the binary search takes O(n). |
| 184 | +
|
| 185 | + |
| 186 | +### Space Complexity: $O(1)$ |
| 187 | + |
| 188 | +> **Reason**: Only a constant amount of extra space is used, regardless of the input size. |
| 189 | +
|
0 commit comments