In this post we are going to solve Birthday Cake Candles HackerRank question.
Problem statement analysis:
The question asks us to find the number of tallest candles that can be blown. So basically it wants us to find the count of tallest candle.
Solutions:
We can solve this in multiple ways lets discuss the different ways in which this can be solved.
Birthday Cake Candles HackerRank Solution 1:
As discuss in problem statement analysis we are required to find the count of tallest candle aka count of maximum number.
So lets use a HashMap to get the count of all elements and then find the maximum element in the array.
Now finally return the count of the maximum element from the Hash Map.
The TimeComplexity of the solution will be O(n) and Space Complexity will be O(n) as we will be using hashmap to store the count of elements.
Code snippet:
public int birthdayCakeCandles(List<Integer> candles) {
HashMap<Integer, Integer> countOfCandles = new HashMap<Integer, Integer>();
for (int i = 0; i < candles.size(); i++) {
if (countOfCandles.containsKey(candles.get(i))) {
countOfCandles.put(candles.get(i), countOfCandles.get(candles.get(i)) + 1);
} else {
countOfCandles.put(candles.get(i), 1);
}
}
int maxCandleValue = 0;
for (int i = 0; i < candles.size(); i++) {
if (candles.get(i) > maxCandleValue) {
maxCandleValue = candles.get(i);
}
}
return countOfCandles.get(maxCandleValue);
}
Birthday Cake Candles HackerRank Solution 2:
So in above solution we have used a hash map to store the count and due to this the space complexity is increased. Lets get some better solution now.
Now instead of using HashMap lets keep the count of max value in variable and also maintain the maximum element.
Code Snippet:
public int birthdayCakeCandles(List<Integer> candles) {
int maxCandleValue = 0;
int maxCandleCount = 0;
for (Integer candle : candles) {
if (candle > maxCandleValue) {
maxCandleValue = candle;
}
}
for (Integer candle : candles) {
if (candle == maxCandleValue) {
maxCandleCount++;
}
}
return maxCandleCount;
}
Here we have reduced the space complexity to O(1) now, but still we can improve it little bit more, as you can see we are looping the list two times, we can optimize it more.
Birthday Cake Candles HackerRank Solution 3:
Instead of looping the list two times we can loop it one time and get he max count by adding if else statements.
Code Snippet:
public int birthdayCakeCandles(List<Integer> candles) {
int maxCandleValue = 0;
int maxCandleCount = 0;
for (Integer candle : candles) {
if (candle == maxCandleValue) {
maxCandleCount++;
} else if (candle > maxCandleValue) {
maxCandleValue = candle;
maxCandleCount = 1;
}
}
return maxCandleCount;
}
Output:
- Mastering Stream APIs in Java: A Comprehensive Guide | KodeSrc
- A Step-by-Step Tutorial on AES 256 Encryption and Decryption in Java using Password by KodeSrc
- Stream API filter() method in Java | KodeSrc
- Demystifying Classes and Objects in Java : Everything You Need to Know | By KodeSrc
- SQL Commands (DQL, DDL, DCL, TCL, and DML) Made Easy | By KodeSrc
Please Let me Know, If you have any doubts.
Please Let me Know, If you have any doubts.