Skip to content

Commit aed6fc9

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank]: Warmup: Birthday Cake Candles solved ✅
1 parent 3765d83 commit aed6fc9

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# [Birthday Cake Candles](https://www.hackerrank.com/challenges/birthday-cake-candles)
2+
3+
Difficulty: #easy
4+
Category: #warmup
5+
6+
You are in charge of the cake for a child's birthday.
7+
You have decided the cake will have one candle for each year of their total
8+
age. They will only be able to blow out the tallest of the candles.
9+
Count how many candles are tallest.
10+
11+
## Example
12+
13+
The maximum height candles are 4 units high.
14+
There are 2 of them, so return 2.
15+
16+
## Function Description
17+
18+
Complete the function birthdayCakeCandles in the editor below.
19+
birthdayCakeCandles has the following parameter(s):
20+
21+
- int candles[n]: the candle heights
22+
23+
## Returns
24+
25+
- int: the number of candles that are tallest
26+
27+
## Input Format
28+
29+
The first line contains a single integer, n, the size of candles[].
30+
The second line contains space-separated integers, where each integer i describes
31+
the height of candles[i].
32+
33+
## Constraints
34+
35+
$ 1 \leq n \leq 10^5 $
36+
$ 1 \leq candles[i] \leq 10^7 $
37+
38+
## Sample Input 0
39+
40+
```text
41+
4
42+
3 2 1 3
43+
```
44+
45+
## Sample Output 0
46+
47+
```text
48+
2
49+
```
50+
51+
## Explanation 0
52+
53+
Candle heights are $ [3, 2, 1, 3] $. The tallest candles are $ 3 $ units, and there
54+
are $ 2 $ of them.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <vector>
2+
3+
#pragma once
4+
5+
namespace hackerrank::warmup {
6+
int birthdayCakeCandles(const std::vector<int>& candles);
7+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <exercises/hackerrank/warmup/a_very_big_sum.hpp>
2+
3+
/**
4+
* @link Problem definition [[docs/hackerrank/warmup/birthday_cake_candles.md]]
5+
*/
6+
7+
#include <numeric>
8+
#include <vector>
9+
10+
namespace hackerrank::warmup {
11+
12+
int birthdayCakeCandles(const std::vector<int>& candles) {
13+
14+
int counter = 0;
15+
int maximum = candles[0];
16+
17+
for (const int& element : candles) {
18+
if (element == maximum) {
19+
counter += 1;
20+
}
21+
if (element > maximum) {
22+
maximum = element;
23+
counter = 1;
24+
}
25+
}
26+
27+
return counter;
28+
}
29+
30+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <catch2/catch_test_macros.hpp>
2+
3+
#include <exercises/hackerrank/warmup/birthday_cake_candles.hpp>
4+
#include <iostream>
5+
#include <vector>
6+
7+
#include <filesystem>
8+
#include <fstream>
9+
#include <nlohmann/json.hpp>
10+
using json = nlohmann::json;
11+
12+
TEST_CASE("birthdayCakeCandles JSON Test Cases", "[warmup]")
13+
{
14+
std::filesystem::path cwd = std::filesystem::current_path();
15+
std::string path = cwd.string() + "/unit/lib/hackerrank/warmup/birthday_cake_candles.testcases.json";
16+
17+
INFO("birthdayCakeCandles JSON test cases FILE: " << path);
18+
19+
std::ifstream f(path);
20+
json data = json::parse(f);
21+
22+
for (auto testcase : data) {
23+
long result = hackerrank::warmup::birthdayCakeCandles(testcase["input"]);
24+
CHECK(result == testcase["expected"]);
25+
}
26+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
{"input": [3, 2, 1, 3], "expected": 2},
3+
{"input": [1, 2, 3, 3], "expected": 2}
4+
]

0 commit comments

Comments
 (0)