Birthday Cake Candles | HackerRank Solution

Hello coders, today we are going to solve Birthday Cake Candles HackerRank Solution which is a Part of Problem Solving Series.

Birthday Cake Candles

Task

You are in charge of the cake for a child’s birthday. You have decided the cake will have one candle for each year of their total age. They will only be able to blow out the tallest of the candles. Count how many candles are tallest.

Example

candles = [4, 4, 1, 3]
The maximum height candles are 4 units high. There are 2 of them, so return 2.

Function Description

Complete the function birthdayCakeCandles in the editor below.

birthdayCakeCandles has the following parameter(s):

  • int candles[n]: the candle heights

Returns

  • int: the number of candles that are tallest

Input Format

The first line contains a single integer, n, the size of candles[].
The second line contains n space-separated integers, where each integer i describes the height of candles[i].

Constraints

  • 1 <= n <= 105
  • 1 <= candles[i] <= 107

Sample Input 0

4
3 2 1 3

Sample Output 0

2

Explanation 0

Candle heights are [3, 2, 1, 3]. The tallest candles are 3 units, and there are 2 of them.

Solution – Birthday Cake Candles

C++

#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);

int birthdayCakeCandles(vector<int> ar) {
    int max = ar[0];
    int count = 0;
    int n = ar.size();

    for(int i=0;i<n; i++) {
        if(ar[i] > max) {
            max = ar[i];
        }        
    }
    for (int i=0;i<n;i++) {
        if(ar[i]==max) {
            count++;
        }            
    }
    return count;
}

int main()
{
    ofstream fout(getenv("OUTPUT_PATH"));

    string candles_count_temp;
    getline(cin, candles_count_temp);

    int candles_count = stoi(ltrim(rtrim(candles_count_temp)));

    string candles_temp_temp;
    getline(cin, candles_temp_temp);

    vector<string> candles_temp = split(rtrim(candles_temp_temp));

    vector<int> candles(candles_count);

    for (int i = 0; i < candles_count; i++) {
        int candles_item = stoi(candles_temp[i]);

        candles[i] = candles_item;
    }

    int result = birthdayCakeCandles(candles);

    fout << result << "\n";

    fout.close();

    return 0;
}

string ltrim(const string &str) {
    string s(str);

    s.erase(
        s.begin(),
        find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
    );

    return s;
}

string rtrim(const string &str) {
    string s(str);

    s.erase(
        find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
        s.end()
    );

    return s;
}

vector<string> split(const string &str) {
    vector<string> tokens;

    string::size_type start = 0;
    string::size_type end = 0;

    while ((end = str.find(" ", start)) != string::npos) {
        tokens.push_back(str.substr(start, end - start));

        start = end + 1;
    }

    tokens.push_back(str.substr(start));

    return tokens;
}

Python

#!/bin/python3

import math
import os
import random
import re
import sys


def birthdayCakeCandles(ar):
    # Write your code here
    count = 0 
    maxHeight = max(ar)
    for i in ar:
        if i == maxHeight:
            count += 1
    return count

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    candles_count = int(input().strip())

    candles = list(map(int, input().rstrip().split()))

    result = birthdayCakeCandles(candles)

    fptr.write(str(result) + '\n')

    fptr.close()

Disclaimer: The above Problem (Birthday Cake Candles) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

1 thought on “Birthday Cake Candles | HackerRank Solution”

  1. Tanyo Kostadinov

    You can eliminate the second for loop like this:

    int birthdayCakeCandles(vector candles) {
    int count = 0;
    int tallest = candles[0];
    for (int i = 0; i tallest) {
    count = 1;
    tallest = candles[i];
    }
    }
    return count;
    }

Leave a Comment

Your email address will not be published. Required fields are marked *