Birthday Cake Candles

Sort by

recency

|

5386 Discussions

|

  • + 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?

  • + 0 comments
    int birthdayCakeCandles(vector<int> candles) {
    set<int> unique_nums(candles.begin(), candles.end());
    int max_val = *max_element(unique_nums.begin(), unique_nums.end());
    int count1 = count(candles.begin(), candles.end(), max_val);
    return count1;
    }
    

    Created OrderedTreeSet to remove the duplicates then find the max element from the set, then find the count of occourence in the provided list.