Skip to content

[Hacker Rank] Warm up: Birthday Cake Candles solved ✓ #169

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
May 27, 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
Original file line number Diff line number Diff line change
@@ -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<Integer> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Integer> candles;
public Integer expected;

public BirthdayCakeCandlesTestCase(List<Integer> _candles, Integer _expected) {
this.candles = _candles;
this.expected = _expected;
}
}

public List<BirthdayCakeCandlesTestCase> 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<Integer> 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));
}
}
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.
Loading