Birthday Cake Candles

Sort by

recency

|

5387 Discussions

|

  • + 0 comments

    Rust

    fn birthdayCakeCandles(candles: &[i32]) -> i32 {
        if candles.is_empty() {
            return 0; 
        }
    
        let mut counter = 0;
        let mut high_number = candles[0];
    
        for &i in candles {
            if i > high_number {
                counter = 1;
                high_number = i;
            } else if i == high_number {
                counter += 1;
            }
        }
    
        counter
    }
    
  • + 0 comments
    return candles.count(max(candles))
    
  • + 0 comments

    Java + based

        public static int birthdayCakeCandles(List<Integer> candles) {
        // Write your code here
    		// Optimized code
    		// Time Complexity O(n) (Linear time)
    		// Space Complexity O(1) (Constant space) Only two integer variables are used, regardless of input size.
            int tallestCandle = 0;
            int tallestCount = 0;
        
            for (Integer i : candles) {
                if(i > tallestCandle) {
                    tallestCandle = i;
                    tallestCount = 0;
                }
                
                if(i == tallestCandle) {
                    tallestCount++;
                }
            }
        
            return tallestCount;
        }
    
    }
    
  • + 0 comments

    def birthdayCakeCandles(candles): # Write your code here result = 0 for num in candles: if candles.count(candles[num]) > 1: result +=1 return result

  • + 0 comments

    That was a clear and simple explanation of the “Birthday Cake Candles” problem. I like how you broke down the logic of finding the tallest candle and counting its occurrences — it really helps beginners understand the reasoning behind the code instead of just following it. It’s interesting how such a basic problem reinforces array traversal and comparison concepts. By the way, have you tried optimizing it using built-in functions or a single-pass approach for larger datasets?