You have n
candies and m
children. The goal is to distribute all the candies among the children such that every child gets exactly one candy. You need to determine if it’s possible to distribute the candies according to the given constraints.
n
is less than m
, should we return False since we can’t give one candy to each child?
n
is a non-integer or m
is a non-integer, how should the function behave?
n
and m
are always non-negative integers.True
if it’s possible to distribute the candies as required, False
otherwise.Given the constraints:
n
should be at least equal to the number of children m
.Thus, the solution involves a simple comparison:
True
if n >= m
.False
otherwise.The time complexity of this approach is (O(1)) because we are performing a single comparison operation.
Here is the implementation in Python:
def canDistributeCandies(n: int, m: int) -> bool:
# Check if we have at least as many candies as children
return n >= m
# Testing the function
print(canDistributeCandies(5, 3)) # Expected: True (5 candies can be distributed among 3 children)
print(canDistributeCandies(4, 5)) # Expected: False (4 candies are not enough for 5 children)
print(canDistributeCandies(10, 10)) # Expected: True (exactly 10 candies for 10 children)
print(canDistributeCandies(0, 0)) # Expected: True (0 candies for 0 children, trivially true)
print(canDistributeCandies(1, 2)) # Expected: False (1 candy is not enough for 2 children)
This solution is efficient and straightforward, perfectly aligning with the constraints and requirements of the problem.
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?