-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbirthday_cake_candles.php
More file actions
34 lines (27 loc) · 1.17 KB
/
birthday_cake_candles.php
File metadata and controls
34 lines (27 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
// Birthday Cake Candles
// You are in charge of the cake for your niece's birthday and have decided the cake will have one candle for each year of her total age.
// When she blows out the candles, she’ll only be able to blow out the tallest ones.
// Your task is to find out how many candles she can successfully blow out.
//
// For example, if your niece is turning 4 years old, and the cake will have 4 candles of height 4, 4, 1, 3,
// she will be able to blow out 2 candles successfully, since the tallest candles are of height 4 and there are 2 such candles.
//
//
// Complete the function birthdayCakeCandles in the editor below. It must return an integer representing the number of candles she can blow out.
//
// birthdayCakeCandles has the following parameter(s):
//
// ar: an array of integers representing candle heights
function birthdayCakeCandles($ar) {
$max = max($ar);
return count(array_filter($ar, function($x) use ($max) {
return $x === $max;
}));
}
// Alternative Solution;
// function birthdayCakeCandles($ar) {
// return array_count_values($ar)[max($ar)];
// }
print_r(birthdayCakeCandles([82, 49, 82, 82, 41, 82, 15, 63, 38, 25])) // 4
?>