Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 667f7d7

Browse files
committed
Add a question
1 parent 990c9ff commit 667f7d7

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

‎README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,3 +1406,43 @@ const removeDuplicates = nums => {
14061406
---
14071407

14081408
**[⬆ Back to Top](#javascript-coding-challenges-for-beginners)**
1409+
1410+
## 43. Kids With the Greatest Number of Candies
1411+
1412+
Given an array `candies` where `candies[i]` represents the number of candies that the `i`th kid has, and an integer `extraCandies`, write a function that for each kid checks if he/she would have the greatest number of candies in the group if they were given `extraCandies`. Note that multiple kids can have the greatest number of candies.
1413+
For example,
1414+
1415+
```
1416+
Input: candies = [2,3,5,1,3], extraCandies = 3
1417+
Output: [true,true,true,false,true]
1418+
1419+
Explanation:
1420+
- Kid 1 has 2 candies and if he or she receives all extra candies (3) will have 5 candies --- the greatest number of candies among the kids.
1421+
- Kid 2 has 3 candies and if he or she receives at least 2 extra candies will have the greatest number of candies among the kids.
1422+
- Kid 3 has 5 candies and this is already the greatest number of candies among the kids.
1423+
- Kid 4 has 1 candy and even if he or she receives all extra candies will only have 4 candies.
1424+
- Kid 5 has 3 candies and if he or she receives at least 2 extra candies will have the greatest number of candies among the kids.
1425+
```
1426+
1427+
```js
1428+
const kidsWithCandies = (candies, extraCandies) => {
1429+
// Your solution
1430+
};
1431+
1432+
console.log(kidsWithCandies([2, 2, 1, 1, 2], 1)); // [true, false, false, false, false];
1433+
```
1434+
1435+
<details><summary>Solution</summary>
1436+
1437+
```js
1438+
const kidsWithCandies = (candies, extraCandies) => {
1439+
const MAX = Math.max(...candies);
1440+
return candies.map(candy => candy + extraCandies >= MAX);
1441+
};
1442+
```
1443+
1444+
</details>
1445+
1446+
---
1447+
1448+
**[⬆ Back to Top](#javascript-coding-challenges-for-beginners)**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /