Leetcode 2928. Distribute Candies Among Children I
You are given an integer array candies
where each candies[i]
represents the number of candies of the i-th
type you have. You are also given an integer k
representing the number of children. You need to determine whether it’s possible to distribute all candies such that each child gets exactly the same number of candies of each type. If it is possible, return true
; otherwise, return false
.
k
?
k
, then it is impossible to distribute them equally, and we should return false
.candies
array or the values within it?
candies.length
≤ 10^4, 1 ≤ candies[i]
≤ 10^7.)k
.k
, return false
.k
, then return true
because it is possible to distribute the candies equally among the children.#include <vector>
class Solution {
public:
bool canDistributeCandies(std::vector<int>& candies, int k) {
for (int candyCount : candies) {
if (candyCount % k != 0) {
return false;
}
}
return true;
}
};
n
is the number of elements in the candies
array. This is because we need to iterate through the entire array to check divisibility for each candy type.This solution efficiently checks the divisibility condition for each type of candy, ensuring the problem constraints and requirements are met.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?