Birthday Cake Candles

Sort by

recency

|

5272 Discussions

|

  • + 0 comments
    int birthdayCakeCandles(int candles_count, int *candles)
    {
    
        int largest = candles[0], same = 0;
        for (int i = 1; i < candles_count; i++)
        {
            if (candles[i] > largest)
            {
                largest = candles[i];
            }
        }
    
        for (int i = 0; i < candles_count; i++)
        {
            if (largest == candles[i])
            {
                same++;
            }
        }
    
        return same;
    }
    
  • + 0 comments

    Here is my c++ solution, you can watch the video explanation here : https://youtu.be/zcWbM-aaopc

    #include <bits/stdc++.h>
    
    using namespace std;
     
    int main (){
        int s;
        cin >> s;
        vector<int> arr(s);
        for(int i = 0; i < s; i++){
            cin >> arr[i];
        }
        cout << count(arr.begin(), arr.end(), *max_element(arr.begin(), arr.end()));    
        return 0;
    }
    
  • + 0 comments

    Solution in Kotlin -

    **1.with Extension function - **

    var maxHeightCandle = candles.maxOrNull()
        var maxHeightCandleCount = 0
        
        for (candle in candles) {
            if (candle == maxHeightCandle) {
                maxHeightCandleCount++
            }
        }
        return maxHeightCandleCount
    

    **2. without Extension function - **

    var maxHeightCandle = candles[0]
        var maxHeightCandleCount = 0
        
        for (candle in candles) {
            if (candle > maxHeightCandle) {
                maxHeightCandle = candle
                maxHeightCandleCount = 1
            } else if (candle == maxHeightCandle) {
                maxHeightCandleCount++
            }
        }
        return maxHeightCandleCount
    
  • + 0 comments
        Java solution:
    
                int max = Collections.max(candles);
        int count = 0;
    
        for (int candle : candles){
            if (candle == max){
                count++;
            }
        }
        return count;
    
  • + 0 comments

    def birthdayCakeCandles(candles): max_candles = max(candles) return candles.count(max_candles)

    This will reduce the time limit