# 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.

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));

}
```

### Python

```#!/bin/python3

import math
import os
import random
import re
import sys

def birthdayCakeCandles(ar):
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.