You are given two positive integers n
and k
, meaning that there are n
children and k
types of candies.
You are given a 0-indexed integer array candies
of length k
where candies[i]
denotes the number of candies of the ith
type you have. You need to distribute all the candies fairly among the n
children.
A distribution of candies is considered fair if each child receives an equal number of candies. More formally, each child must receive a total of totalCandies / n
candies, where totalCandies
is the sum of all candies you have.
Return true
if it is possible to distribute all the candies fairly among the n
children. Otherwise, return false
.
Example 1:
Input: n = 3, candies = [5,8] Output: true Explanation: You have 5 candies of type 1 and 8 candies of type 2. In total, you have 5 + 8 = 13 candies. You can give 4 candies of type 2 to the first child so that they have 4 candies in total. You can give 4 candies of type 2 to the second child so that they have 4 candies in total. Finally, you can give 5 candies of type 1 to the third child so that they have 5 candies in total.
Example 2:
Input: n = 4, candies = [1,2,3,4] Output: false Explanation: You have 1 candy of type 1, 2 candies of type 2, 3 candies of type 3, and 4 candies of type 4. In total, you have 1 + 2 + 3 + 4 = 10 candies. It is impossible to give each of the 4 children exactly 10 / 4 = 2.5 candies each since you can only give a child a whole number of candies.
Constraints:
1 <= n <= 107
1 <= candies.length <= 105
1 <= candies[i] <= 109
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to distributing candies is like trying every single way to give candies to the children. We explore every possible combination, checking if each one meets the rules of the candy distribution.
Here's how the algorithm would work step-by-step:
def distribute_candies_brute_force(number_of_candies, number_of_children):
best_distribution = None
def find_distributions(current_child_index, remaining_candies, current_distribution):
nonlocal best_distribution
# Base case: All children have been considered
if current_child_index == number_of_children:
# Check if all candies have been distributed
if remaining_candies == 0:
# Update the best distribution if necessary
if best_distribution is None or current_distribution[-1] > best_distribution[-1]:
best_distribution = current_distribution.copy()
return
# Iterate through possible candy amounts for the current child
for candies_for_current_child in range(remaining_candies + 1):
# Recursive call to handle the next child
find_distributions(
current_child_index + 1,
remaining_candies - candies_for_current_child,
current_distribution + [candies_for_current_child],
)
# Start the recursive process
find_distributions(0, number_of_candies, [])
return best_distribution
The most efficient way to distribute candies fairly involves giving each child the minimum possible, then sharing the extras in a way that limits the biggest difference between any two children. This avoids unnecessary calculations by focusing on fairness and minimizing the spread.
Here's how the algorithm would work step-by-step:
def distribute_candies(number_of_candies, number_of_children):
base_candies = number_of_candies // number_of_children
candies_distributed = [base_candies] * number_of_children
remaining_candies = number_of_candies - base_candies * number_of_children
# Distribute remaining candies to minimize difference
child_index = 0
while remaining_candies > 0:
candies_distributed[child_index] += 1
remaining_candies -= 1
# Avoid giving adjacent children extra candies
child_index += 1
if child_index >= number_of_children:
child_index = 0
return candies_distributed
Case | How to Handle |
---|---|
Number of children is zero or negative. | Return 0 since candies cannot be distributed to no or negative children. |
Number of candies is zero. | Return 0 because no candies can be distributed. |
Number of candies is less than the number of children. | Return 0 as not every child can receive a candy. |
Number of candies is a very large number, potentially leading to integer overflow if not handled correctly. | Use a data type that can accommodate large numbers (e.g., long in Java) to prevent overflow during calculations. |
Number of candies is equal to the maximum integer value. | Ensure calculations involving the number of candies do not cause an integer overflow. |
The problem specifies that candies are distributed *equally*, but the number of candies is not divisible by the number of children. | The prompt did not specify equal distribution, so return the floor of candies/children to find how many candies each child gets. |
Extremely large number of children and candies, possibly exceeding memory limitations if intermediate calculations store large data structures. | Optimize memory usage, perhaps by performing calculations iteratively instead of storing large intermediate results, or considering a formula to calculate the final result without extensive memory. |
If there are constraints or conditions on how many maximum candies each child can receive | If there is a constraint that each child cannot receive more than X candies then return minimum of (floor(candies/children) , X). |