Skip to content

Commit 522bf6d

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank]: Warmup: Birthday Cake Candles solved ✅
1 parent ec959b8 commit 522bf6d

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <vector>
2+
3+
#pragma once
4+
5+
#ifdef __cplusplus
6+
extern "C" {
7+
#endif
8+
9+
int HACKERRANK_WARMUP_birthdayCakeCandles(int candles_count,
10+
const int *candles);
11+
12+
#ifdef __cplusplus
13+
} // extern "C"
14+
#endif
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <exercises/hackerrank/warmup/a_very_big_sum.h>
2+
3+
/**
4+
* @link Problem definition [[docs/hackerrank/warmup/birthday_cake_candles.md]]
5+
*/
6+
7+
int HACKERRANK_WARMUP_birthdayCakeCandles(int candles_count,
8+
const int *candles) {
9+
int counter = 0;
10+
int maximum = candles[0];
11+
12+
for (int i = 0; i < candles_count; i++) {
13+
if (candles[i] == maximum) {
14+
counter += 1;
15+
}
16+
if (candles[i] > maximum) {
17+
maximum = candles[i];
18+
counter = 1;
19+
}
20+
}
21+
22+
return counter;
23+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <catch2/catch_test_macros.hpp>
2+
3+
#include <exercises/hackerrank/warmup/birthday_cake_candles.h>
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",
13+
"[hackerrank] [jsontestcase] [warmup]") {
14+
std::filesystem::path cwd = std::filesystem::current_path();
15+
std::string path =
16+
cwd.string() +
17+
"/unit/lib/hackerrank/warmup/birthday_cake_candles.testcases.json";
18+
19+
INFO("birthdayCakeCandles JSON test cases FILE: " << path);
20+
21+
std::ifstream f(path);
22+
json data = json::parse(f);
23+
24+
for (auto testcase : data) {
25+
auto input_size = static_cast<int>(testcase["input"].size());
26+
std::vector<int> input_vector = testcase["input"];
27+
const int *input_array = input_vector.data();
28+
29+
long result =
30+
HACKERRANK_WARMUP_birthdayCakeCandles(input_size, input_array);
31+
CHECK(result == testcase["expected"]);
32+
}
33+
}
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)