Objective:
Determine the number of candles on a birthday cake that are the tallest. This problem concerns finding and counting the tallest birthday cake candles in a given list of candle heights.
Problem Statement:
You’ve decided to place one candle on the cake for each year of their age, with the tallest candle being the one they will blow out. Count how many candles are tallest”. This means here you are given a list of integers representing the heights of birthday cake ‘candles’. Your task is to determine how many candles are of the tallest height.
Input Format:
- An integer
n
representing the number of birthday cake candles. - A list of
n
integers where each integer represents the height of a candle.
Output Format:
- A single integer representing the number of tallest birthday cake candles.
Example 1:
- Input:
- n = 4
- candles = [3, 2, 1, 3]
- Output:
- 2 (since there are two birthday cake candles of height 3, which is the tallest height)
Example 2:
- Input:
- n = 5
- candles = [5, 5, 1, 3, 5, 5]
- Output:
- 4 (since there are three birthday cake candles of height 5, which is the tallest height)
Example 3:
- Input:
n =
7- candles = [1, 2, 1, 2, 1, 2, 1]
- Output:
- 3 (since there are three birthday cake candles of height 2, which is the tallest height)
Steps to Solve:
- Identify the Tallest Candle: Find the maximum height among the candles.
- Count the Tallest Candles: Count how many candles have this maximum height.
Birthday Cake Candles Problem Snippet
Solution
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'birthdayCakeCandles' function below.
*
* The function is should return an INTEGER.
* The function accepts INTEGER_ARRAY candles as parameter.
*/
public static int birthdayCakeCandles(List<Integer> candles) {
// Write your code here
Collections.sort(candles, Comparator.reverseOrder());
int count = 0;
int store = 0;
if (!candles.isEmpty()) {
count = 1;
store = candles.get(0);
for (int i = 1; i < candles.size(); i++)
if (candles.get(i) == store) count++;
}
return count;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int candlesCount = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> candles = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
int result = Result.birthdayCakeCandles(candles);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}