# How To Solve Birthday cake candles HackerRank Problem Hello everyone welcomes to my YouTube channel and
today we are going to solve birthday cake candles hackerrank problem. So, this is the problem description, 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 will only
be able to blow out the tallest ones, your task is to find out how many candles she can
successfully blow out. So guys, let me summarize this problem in
short, we have given an array which contains the height of the candles and the niece can
blow out only the candle which is having a maximum height. So, let’s see this example, if your niece is
turning 4 years old and the cake will have 4 candles of height 4,4,1 and 3. She will be able to blow out 2 candles. like I said, if you have different types of
candles but she will be only able to blow out the candles with the maximum height. So we can see maximum height in this array
is 4 and we have 4 twice means we have two candles with the maximum height. So she can only blow out the 2 candles which
is the answer. so this is the problem guys, let’s see the
input format. so in the input, the first line contains a
single integer n denoting the number of candles on the cake. The second line contains n space separated
integers where each integer i describe the height of the candle i. So, we can see guys, here 4 represent the
number of candles present in the array. And these are the 4 candles which are having
different heights. and our job is to find out how many candles she can blow out. As we have gone through the problem, we know
she can blow out the candle which has a maximum height. So in the array we have maximum height is
3 and that maximum height is present two times. Or We can say, we have two candles which are
having the maximum height. She can only blow out the two candles. I hope guys this problem is clear. now try
to build the logic around it. Le’ts assume we have an array of size 5
candles and these are the 5 candles. Our task is to identify how many candles she
can blow out as you know, We have discussed she can blow out only the candles which is
having the maximum height . So, we can see the maximum height is 4 in
this array and we have two candles which are having height 4 so she can only blow 2 candles. but how did you find that. so the algorithm
will be something like this. Step 1, find the maximum from the array.so what
we have done. We have found the maximum from the array. In the second step, count the frequency of
the maximum element. so what we’ve done guys, we have just taken
a maximum element from this array which is 4 and count the occurrence or frequency of
4 inside the array. So, we can see we have 2 occurrences of 4
that’s why the answer is 2.So the algorithm is very sweet and simple guys. So, you have to do two things here we have
to just find the maximum element from the array and after that, we have to count a number
of occurrences of that element. So finding the maximum element from the array
requires for loop, in that we have to go through all the elements and we can find the maximum. in the same way, counting the frequency of
the particular element also requires one for loop so in that we can count the frequency. As we know for counting the maximum we apply this
logic. We have array.if array element is greater
than maximum so the maximum will be the array element So this is the logic we always used to get
the maximum element from the array which will be inside one for a loop. And to count the frequency of maximum, we
have to use if the array element is equal to the max element. so we have to increase the frequency count. So guys, to find out the maximum in a loop
we have to apply the logic something like this. Which will get the maximum value from the
array.and in the second loop we have to apply logic Similar to this which will just check
how many frequency or occurrences are there in an array. And
If you don’t want to write a separate loop for this task you can club this two task in
a one for loop, and solve this problem. let’s see the algorithm in action guys. so here we have one method called birthdayCakeCandles
and we are accepting input as an array and we have taken two variable here. one will maintain the maximum candle height
and the second will maintain the frequency count of the maximum candle height. so, guys, this is the for loop where we are
traversing throughout the array. and we have applied the same condition which I have shown
you earlier. So if the current element equal to the maximum
candle height. So we are assuming the maximum candle height
initially will be the minimum value. but if we find a current value which is equal
equals to the maximum candle height. We will just increment the max candle frequency
count. Or you can see, we are just incrementing the
frequency count of the maximum candle height. and At the same time, we also keep on checking
whether the current array element is greater than the maximum candle height.It means the
current element is having the height which is greater than the maximum candle heights so far. if this the case, its true.we are just swapping
the maximum candle height and assigning maximum candle frequency count to 1.Because this is
the first time we got this element or candle which is having the maximum height. so here we are assigning it to 1.Because this
is the first time we got this candle with the maximum height. And from the next time, we found the same
candle we are just incrementing this maximum frequency count. So once the loop got over. we are returning maximum candle frequency
count. guys if you have any kind of confusion or
doubt How this is happening inside our loop. I will suggest you create two for a loop. One for loop just for finding the maximum
height where you will have this logic and another loop once you get this is the element
which is having a maximum height you can just traverse through for loop and return the frequency
count of that element. Or you can say the maximum height of the candle. I hope guys, that is clear to you now we will
just try to run this algorithm on the hackerrank platform. So guys here I am just directly submitting
my code because I know it will work as I have already solved this earlier. Congratulations, so you can see guys all test
case got passed. I hope this problem is clear to you guys, how did you solve this problem,
guys if you want the source code of this problem, you can go to my git hub repository and the
link is mentioned in the description box. please don’t forget like comment share and
subscribe friends and let me know if you have any better solution to the problem. Thanks for watching guys.

1. Hello Coding Lover,

If you have any doubts or any better approach to solve this problem, let me know in comments.

JavaAid

2. Good job. I don't have the sound on so I don't know how that integer.MIN_VALUE works. Nice one.
I wrote one in JavaScript:

function birthdayCakeCandles(ar) {
ar.sort((a, b) => b – a);
let m = 0;
let k = ar;
while (parseInt(ar[m]) === parseInt(k)){
m++;
}
return m;
}

3. Hey. Great explanation.
Although I think you can easily assume that maxHandleHeight = ar; and frequency = 1. Then you can start your loop from 1, not 0. and if you find any bigger value than your current maxHandleHeight you just assign this new value and set frequency = 0.

4. Thankyou for your video, really helping! Keep up the good work!

5. php solution
————————
<?php

\$a = array(3,2,4,2,4);

\$max = max(\$a);

\$b= array_count_values(\$a)[\$max];

echo \$max.'<br>'.\$b;

?>

6. Hey, I just want to take a moment to thank you for taking the time to create these videos. your approach is second to none.