Birthday Cake Candles

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