# Birthday Cake Candles – HackerRank Solution ## Problem :

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  candles successfully, since the tallest candles are of height `4` and there are `2` such candles.

### Function Description :

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

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

### Constraints :

• `1 <= n <= 105`
• `1 <= ar[i] <= 107`

### Output Format :

Return the number of candles that can be blown out on a new line.

### Sample Input 0 :

``````4
3 2 1 3``````

## Sample Output 0 :

``2``

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

## Solution :

``````#include <bits/stdc++.h>

using namespace std;

vector<string> split_string(string);

// Complete the birthdayCakeCandles function below.
int birthdayCakeCandles(vector<int> ar) {
int count = 0;
int max = *max_element(ar.begin(),ar.end());

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

}

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

int ar_count;
cin >> ar_count;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

string ar_temp_temp;
getline(cin, ar_temp_temp);

vector<string> ar_temp = split_string(ar_temp_temp);

vector<int> ar(ar_count);

for (int i = 0; i < ar_count; i++) {
int ar_item = stoi(ar_temp[i]);

ar[i] = ar_item;
}

int result = birthdayCakeCandles(ar);

fout << result << "\n";

fout.close();

return 0;
}

vector<string> split_string(string input_string) {
string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
return x == y and x == ' ';
});

input_string.erase(new_end, input_string.end());

while (input_string[input_string.length() - 1] == ' ') {
input_string.pop_back();
}

vector<string> splits;
char delimiter = ' ';

size_t i = 0;
size_t pos = input_string.find(delimiter);

while (pos != string::npos) {
splits.push_back(input_string.substr(i, pos - i));

i = pos + 1;
pos = input_string.find(delimiter, i);
}

splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));

return splits;
}
``````

155 total views,  1 views today

#### Post Disclaimer

the above hole problem statement is given by hackerrank.com but the solution is generated by the SLTECHACADEMY authority if any of the query regarding this post or website fill the following contact form thank you.