Kids With the Greatest Number of Candies

Easy · Arrays & Hashing · Array

Problem

There are n kids, and kid i has candies[i] candies. You also hold extraCandies candies, which you could hand, all of them, to a single kid.

For every kid, decide whether giving them all the extra candies would leave them with the greatest number of candies among all the kids. Ties count: several kids may share the greatest number.

Return a list of booleans result of length n, where result[i] is True when kid i could end up with the most, and False otherwise. Each kid is considered on their own: the other kids keep only their own candies.

Examples

Input: candies = [2, 3, 5, 1, 3], extraCandies = 3
Output: [True, True, True, False, True]

The current maximum is 5. Kid 3 would reach only 1 + 3 = 4; every other kid reaches at least 5.

Input: candies = [4, 2, 1, 1, 2], extraCandies = 1
Output: [True, False, False, False, False]

Only kid 0, who already has 4, stays at the top; the next best would reach 3.

Input: candies = [12, 1, 12], extraCandies = 10
Output: [True, False, True]

Kid 1 reaches 11, one short of 12.

Constraints

Approaches

Follow-up

How many extra candies would each kid need to lead on their own, strictly ahead of everyone? And what is the smallest gift that would let at least k kids be among the greatest?

Related problems

Start free on Cariara · Coding problems · System design studio

Starting Python
No results yet.

          
↑↓ move ↵ open esc close

Labs

Loading labs…

Run with your own input

Starting Python

Loading Python…