Birthday Cake Candles challenge and working with an Array List
Tags: java
Published: 2021-04-19 22:26:55

I am learning Java, and wanted to see if I was able to do some HackerRank problems. Do keep in mind that I am going straight to the solving this and not worrying about all that other stuff in their editor.

When solving this I had to do a quick lookup of array list as I was not that far into my lessons yet in Java. The thing I love about them is that they are mutable versus a regular Java array is immutable. Its safe to assume that these are used more often than regular arrays in Java. Anyone can feel free to call my out and correct me on this.

The problem states...

*You are in charge of the cake for your 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 are the tallest.*

So my thought process...."When did I care how tall my candles were on my cakes because I blew them all out"....jk

I first had to figure out what the output was supposed to give me. When I was examining the examples provided, it was easier to understand.

The input would accept an array list of integers as such....

```
[1,5,6,6]
```

Then they get accepted into a method and the array list is the argument accepted into the parameters of the method. 

```
public static int birthdayCakeCandles(List<Integer> candles) {

}
```

It gets fun from here...

How I broke this down from this point is I had to figure how to scan through each element by looping to see if the element meets a certain condition.

First I had to declare a variable that has the first element of the array list assigned to it because the assumption is that this is the tallest until otherwise.

```
public static int birthdayCakeCandles(List<Integer> candles) {
 int tallest = candles.get(0)

}
```

Next part is I need come kind of count since it asking me for the number of the tallest candles. I will declare a variable called "sum" that will keep track of this count

```
public static int birthdayCakeCandles(List<Integer> candles) {
 int tallest = candles.get(0)
 int sum = 0


}
```

From here I was faced with two small unknowns.... I needed to determine what candle(s) is/are the tallest of the candles, then I can count them from there. For this I implemented two for loops checking for those conditions separately before I was given a final count.

```
public static int birthdayCakeCandles(List<Integer> candles) {
 int tallest = candles.get(0)
 int sum = 0
//If the i value is greater than the inital tallest...tallest is reassigned that value until another values get assigned or not.

for (Integer i : candles){
        if (tallest < i){
            tallest = i;
        }     
        
    }


}

```

Next you will keep track of the count of the tallest by telling your for loop to increment the count if i is equal to the current tallest value


```
public static int birthdayCakeCandles(List<Integer> candles) {
 int tallest = candles.get(0)
 int sum = 0

for (Integer i : candles){
        if (tallest < i){
            tallest = i;
        }     
        
    }

for (Integer i : candles){
        if (i == tallest){
        sum += 1;
        }
        
    }


}

```

Then last ,but not least.....you will return the sum


```
public static int birthdayCakeCandles(List<Integer> candles) {
    
    int highest = candles.get(0);
    int sum = 0;
    for (Integer i : candles){
        if (highest < i){
            highest = i;
        }     
        
    }
    
    for (Integer i : candles){
        if (i == highest){
        sum += 1;
        }
        
    }
    
    
      return sum;//should return 2 if we are using the array from earlier

    }
    
    
}
```

And this is how I approached and solved the Birthday Cake Candles problem.