Birthday Cake Candles Hackerrank problem solution -coderInme

Birthday Cake Candles Hackerrank problem solution -coderInme

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 3, 2, 1, 3, she will be able to blow out 2 candles successfully, since the tallest candle is of height 3 and there are 2 such candles.

Complete the function birthdayCakeCandles that takes your niece’s age and an integer array containing height of each candle as input, and return the number of candles she can successfully blow out.

Input Format

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 describes the height of candle i.


1<=height i<=107

Output Format

Print the number of candles Colleen blows out.

Sample Input 0

3 2 1 3

Sample Output 0


Explanation 0

We have one candle of height 1, one candle of height 2, and two candles of height 3. Your niece only blows out the tallest candles, meaning the candles where height=3. Because there are 2 such candles, we print 2 on a new line.

include <iostream>

int main(){
    int c, n, max = 0;
    while(std::cin >> n)
        max < n ? c = !!(max = n) : c += max == n;
    std::cout << c;
    return 0;

more easy answer

while(std::cin >> n){ // for each candle n check:
    if(max < n) { // does n set a new record in height?
        max = n; // if that's so, then n is the new max height
        c = 1; // and the counter c must be set to 1 again. "!!" is a cheap trick to convert any value different from 0 into 1
    else { // otherwise check if the new candle is as tall as max
        if (max == n)
            c++; // in that case, add 1 to counter (otherwise add 0)

If you want to use function then

int birthdayCakeCandles(int n, vector <int> ar) {
    int count = 1;
    sort(ar.begin(), ar.end());
    reverse(ar.begin(), ar.end());
    for (int i = 0; i < n; i++){
        if (ar[i] == ar[i+1]){
    return count;
Python solution
n = input()
arr = map(int, raw_input().split())
print arr.count(max(arr))

python is always the best

return ar.count(max(ar))


import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(;
        // the number of candles
        int n = scan.nextInt();
        // store the current maximum height of any candle, initialize to the minimum possible height of any candle
        int maxHeight = 1; 
        // count the number of candles having the maximum height
        int countMax = 0;
        for(int i = 0; i < n; i++) {
            int tmp = scan.nextInt();
            // if you read in a value larger than maxHeight, set new maxHeight
            if(tmp > maxHeight) {
                maxHeight = tmp;
                countMax = 1;
            // if you read a value equal to the current value of maxHeight
            else if(tmp == maxHeight) {
            	// increment the count of candles having maximum height

A web developer(Front end and Back end), and DBA at Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me

Leave a reply:

Your email address will not be published.