Birthday Cake Candles

Sort by

recency

|

5233 Discussions

|

  • + 0 comments

    Python

    return candles.count(max(candles))

  • + 0 comments

    Golang

    func birthdayCakeCandles(candles []int32) int32 {
        // Write your code here
        n := len(candles)
        if n <=0 {
            return 0
        }
        
        maxCount :=1
        maxValue := candles[0]
        for i:=1;i<n;i++ {
            if candles[i]>maxValue {
                // reset the counter and update the new max value
                maxCount = 1
                maxValue = candles[i]
            } else if candles[i] == maxValue {
                maxCount++
            }
        }
        return int32(maxCount)
    }
    
  • + 0 comments
        int count = 0;
    int max = 0;
    for(int a: candles){
        if(a>max){
            max = a;
        }
    }
    for(int a: candles){
        if(a == max){
            count++;
        }
    }   
    return count;
    
  • + 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

    C# or csharp solution: public static int birthdayCakeCandles(List candles) { int max = candles.Max(); int count = 0;

        foreach(int i in candles)
        {
            if (i == max)
                count++;
        }
    
        return count;
    }
    
    Now I'm just getting into Big O notation, so if any other commenters could help me figure out the Big O of this algorithm, it would be greatly appreciated.
    

    I'm thinking it would be O(2n) - since candles.Max() presumably is looping through the array once and then my foreach loop will iterate through the array once more.