Skip to content

[Hacker Rank]: Warmup: Birthday Cake Candles solved ✅ #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions docs/hackerrank/warmup/birthday_cake_candles.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# [Birthday Cake Candles](https://www.hackerrank.com/challenges/birthday-cake-candles)

Difficulty: #easy
Category: #warmup

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

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 space-separated integers, where each integer i describes
the height of candles[i].

## Constraints

$ 1 \leq n \leq 10^5 $
$ 1 \leq candles[i] \leq 10^7 $

## Sample Input 0

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

## Sample Output 0

```text
2
```

## Explanation 0

Candle heights are $ [3, 2, 1, 3] $. The tallest candles are $ 3 $ units, and there
are $ 2 $ of them.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <vector>

#pragma once

namespace hackerrank::warmup {
int birthdayCakeCandles(const std::vector<int>& candles);
}
27 changes: 27 additions & 0 deletions src/lib/exercises/src/hackerrank/warmup/birthday_cake_candles.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <exercises/hackerrank/warmup/a_very_big_sum.hpp>

/**
* @link Problem definition [[docs/hackerrank/warmup/birthday_cake_candles.md]]
*/

#include <numeric>
#include <vector>

namespace hackerrank::warmup {
int birthdayCakeCandles(const std::vector<int>& candles) {
int counter = 0;
int maximum = candles[0];

for (const int& element : candles) {
if (element == maximum) {
counter += 1;
}
if (element > maximum) {
maximum = element;
counter = 1;
}
}

return counter;
}
}
26 changes: 26 additions & 0 deletions src/tests/unit/lib/hackerrank/warmup/birthday_cake_candles.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <catch2/catch_test_macros.hpp>

#include <exercises/hackerrank/warmup/birthday_cake_candles.hpp>
#include <iostream>
#include <vector>

#include <filesystem>
#include <fstream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;

TEST_CASE("birthdayCakeCandles JSON Test Cases", "[warmup]")
{
std::filesystem::path cwd = std::filesystem::current_path();
std::string path = cwd.string() + "/unit/lib/hackerrank/warmup/birthday_cake_candles.testcases.json";

INFO("birthdayCakeCandles JSON test cases FILE: " << path);

std::ifstream f(path);
json data = json::parse(f);

for (auto testcase : data) {
long result = hackerrank::warmup::birthdayCakeCandles(testcase["input"]);
CHECK(result == testcase["expected"]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
{"input": [3, 2, 1, 3], "expected": 2},
{"input": [1, 2, 3, 3], "expected": 2}
]