diff --git a/algorithm-exercises-java/src/main/java/ae/hackerrank/warmup/BirthdayCakeCandles.java b/algorithm-exercises-java/src/main/java/ae/hackerrank/warmup/BirthdayCakeCandles.java new file mode 100644 index 0000000..ebe2bc0 --- /dev/null +++ b/algorithm-exercises-java/src/main/java/ae/hackerrank/warmup/BirthdayCakeCandles.java @@ -0,0 +1,41 @@ +package ae.hackerrank.warmup; + +import java.util.List; + +/** + * Birthday Cake Candles. + * + * @link Problem definition [[docs/hackerrank/warmup/solve_me_first.md]] + */ +public class BirthdayCakeCandles { + + private BirthdayCakeCandles() { + } + + static java.util.logging.Logger logger = util.CustomLogger.getLogger(); + + /** + * birthdayCakeCandles. + */ + public static int birthdayCakeCandles(List candles) { + if (candles == null || candles.isEmpty()) { + throw new IllegalArgumentException("Parameter cannot be empty"); + } + + int counter = 0; + int maximum = candles.get(0); + + for (Integer element : candles) { + if (element > maximum) { + maximum = element; + counter = 1; + } else { + if (element == maximum) { + counter += 1; + } + } + } + + return counter; + } +} diff --git a/algorithm-exercises-java/src/test/java/ae/hackerrank/warmup/BirthdayCakeCandlesTest.java b/algorithm-exercises-java/src/test/java/ae/hackerrank/warmup/BirthdayCakeCandlesTest.java new file mode 100644 index 0000000..acb7bde --- /dev/null +++ b/algorithm-exercises-java/src/test/java/ae/hackerrank/warmup/BirthdayCakeCandlesTest.java @@ -0,0 +1,82 @@ +package ae.hackerrank.warmup; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; + + +@TestInstance(Lifecycle.PER_CLASS) +class BirthdayCakeCandlesTest { + public class BirthdayCakeCandlesTestCase { + public List candles; + public Integer expected; + + public BirthdayCakeCandlesTestCase(List _candles, Integer _expected) { + this.candles = _candles; + this.expected = _expected; + } + } + + public List testCases; + + @BeforeAll + public void setup() { + this.testCases = Arrays.asList( + new BirthdayCakeCandlesTestCase( + Arrays.asList(3, 2, 1, 3), + 2 + ), + new BirthdayCakeCandlesTestCase( + Arrays.asList(1, 2, 3, 3), + 2 + ) + ); + } + + @Test void birthdayCakeCandles() { + for (BirthdayCakeCandlesTestCase testCase : this.testCases) { + Integer solutionFound = BirthdayCakeCandles.birthdayCakeCandles(testCase.candles); + + assertEquals(testCase.expected, solutionFound, + String.format("%s(%s) answer must be: %d", + "CompareTriplets.compareTriplets", + testCase.candles.toString(), + testCase.expected) + ); + } + } + + @Test + void testMiniMaxSumNullInput() { + + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + BirthdayCakeCandles.birthdayCakeCandles(null); + }); + + String expectedMessage = "Parameter cannot be empty"; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.contains(expectedMessage)); + } + + @Test + void testMiniMaxSumEmptyInput() { + List input = Arrays.asList(); + + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + BirthdayCakeCandles.birthdayCakeCandles(input); + }); + + String expectedMessage = "Parameter cannot be empty"; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.contains(expectedMessage)); + } +} 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.