diff --git a/algorithm-exercises-csharp-test/src/hackerrank/warmup/BirthdayCakeCandles.Test.cs b/algorithm-exercises-csharp-test/src/hackerrank/warmup/BirthdayCakeCandles.Test.cs new file mode 100644 index 0000000..1d1b874 --- /dev/null +++ b/algorithm-exercises-csharp-test/src/hackerrank/warmup/BirthdayCakeCandles.Test.cs @@ -0,0 +1,35 @@ +namespace algorithm_exercises_csharp.hackerrank; + +[TestClass] +public class BirthdayCakeCandlesTest +{ + public class BirthdayCakeCandlesTestCase + { + public List input = []; + public int expected = 0; + } + + private static readonly BirthdayCakeCandlesTestCase[] tests = [ + new() { input = [3, 2, 1, 3], expected = 2 }, + new() { input = [1, 2, 3, 3], expected = 2 } + ]; + + [TestMethod] + public void testBirthdayCakeCandles() + { + int? result; + + foreach (BirthdayCakeCandlesTestCase test in tests) + { + result = BirthdayCakeCandles.birthdayCakeCandles(test.input); + Assert.AreEqual(test.expected, result); + } + } + + [TestMethod] + public void testMiniMaxSumEmptyInput() + { + Assert.ThrowsException(() => BirthdayCakeCandles.birthdayCakeCandles([])); + } +} + diff --git a/algorithm-exercises-csharp/src/hackerrank/warmup/BirthdayCakeCandles.cs b/algorithm-exercises-csharp/src/hackerrank/warmup/BirthdayCakeCandles.cs new file mode 100644 index 0000000..2b15a21 --- /dev/null +++ b/algorithm-exercises-csharp/src/hackerrank/warmup/BirthdayCakeCandles.cs @@ -0,0 +1,42 @@ +// @link Problem definition [[docs/hackerrank/warmup/birthday_cake_candles.md]] + +namespace algorithm_exercises_csharp.hackerrank; + +using System.Diagnostics.CodeAnalysis; +using System.Linq.Expressions; + +public class BirthdayCakeCandles +{ + [ExcludeFromCodeCoverage] + protected BirthdayCakeCandles() { } + + public static int birthdayCakeCandles(List _arr) + { + if (_arr.Count == 0) + { + throw new ArgumentException("Parameter cannot be empty", nameof(_arr)); + } + + int counter = 0; + int maximum = _arr[0]; + + foreach (int element in _arr) + { + if (element > maximum) + { + maximum = element; + counter = 1; + } + else + { + if (element == maximum) + { + counter += 1; + } + } + } + + return counter; + } + +} diff --git a/docs/hackerrank/warmup/birthday_cake_candles.md b/docs/hackerrank/warmup/birthday_cake_candles.md new file mode 100644 index 0000000..8e8b3ef --- /dev/null +++ b/docs/hackerrank/warmup/birthday_cake_candles.md @@ -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.