You are given an integer array candies
, where each candies[i]
represents the number of candies the i-th
kid has, and an integer extraCandies
, denoting the number of extra candies you have.
Return a boolean array result
where result[i]
is True
if, after giving the i-th
kid all the extraCandies
, they will have the greatest number of candies among all the kids, or False
otherwise.
Input: candies = [2, 3, 5, 1, 3], extraCandies = 3
Output: [True, True, True, False, True]
Explanation:
Kid 1: 2 + 3 = 5, greatest number of candies.
Kid 2: 3 + 3 = 6, greatest number of candies.
Kid 3: 5 + 3 = 8, greatest number of candies.
Kid 4: 1 + 3 = 4, not greatest number of candies.
Kid 5: 3 + 3 = 6, greatest number of candies.
candies
array?
1 <= candies.length <= 100
and 0 <= candies[i] <= 100
.extraCandies
?
1 <= extraCandies <= 50
.extraCandies
.True
if it is, otherwise record False
.def kids_with_candies(candies, extraCandies):
max_candies = max(candies) # Find the maximum number of candies any kid currently has
result = []
for candy in candies:
# Check if giving current kid all the extraCandies makes their total >= max_candies
if candy + extraCandies >= max_candies:
result.append(True)
else:
result.append(False)
return result
# Example usage
candies = [2, 3, 5, 1, 3]
extraCandies = 3
print(kids_with_candies(candies, extraCandies)) # Output: [True, True, True, False, True]
extraCandies
is greater than or equal to 5.This approach ensures we efficiently determine which kids can have the greatest number of candies by distributing the extra candies.
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?