Skip to content

Commit d7e095a

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank]: Warmup: Birthday Cake Candles solved ✓
1 parent 83f8700 commit d7e095a

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
namespace algorithm_exercises_csharp.hackerrank;
2+
3+
[TestClass]
4+
public class BirthdayCakeCandlesTest
5+
{
6+
public class BirthdayCakeCandlesTestCase
7+
{
8+
public List<int> input = [];
9+
public int expected = 0;
10+
}
11+
12+
private static readonly BirthdayCakeCandlesTestCase[] tests = [
13+
new() { input = [3, 2, 1, 3], expected = 2 },
14+
new() { input = [1, 2, 3, 3], expected = 2 }
15+
];
16+
17+
[TestMethod]
18+
public void testBirthdayCakeCandles()
19+
{
20+
int? result;
21+
22+
foreach (BirthdayCakeCandlesTestCase test in tests)
23+
{
24+
result = BirthdayCakeCandles.birthdayCakeCandles(test.input);
25+
Assert.AreEqual(test.expected, result);
26+
}
27+
}
28+
29+
[TestMethod]
30+
public void testMiniMaxSumEmptyInput()
31+
{
32+
Assert.ThrowsException<ArgumentException>(() => BirthdayCakeCandles.birthdayCakeCandles([]));
33+
}
34+
}
35+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// @link Problem definition [[docs/hackerrank/warmup/birthday_cake_candles.md]]
2+
3+
namespace algorithm_exercises_csharp.hackerrank;
4+
5+
using System.Diagnostics.CodeAnalysis;
6+
using System.Linq.Expressions;
7+
8+
public class BirthdayCakeCandles
9+
{
10+
[ExcludeFromCodeCoverage]
11+
protected BirthdayCakeCandles() { }
12+
13+
public static int birthdayCakeCandles(List<int> _arr)
14+
{
15+
if (_arr.Count == 0)
16+
{
17+
throw new ArgumentException("Parameter cannot be empty", nameof(_arr));
18+
}
19+
20+
int counter = 0;
21+
int maximum = _arr[0];
22+
23+
foreach (int element in _arr)
24+
{
25+
if (element > maximum)
26+
{
27+
maximum = element;
28+
counter = 1;
29+
}
30+
else
31+
{
32+
if (element == maximum)
33+
{
34+
counter += 1;
35+
}
36+
}
37+
}
38+
39+
return counter;
40+
}
41+
42+
}
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.

0 commit comments

Comments
 (0)