In this HackerRank Birthday Cake Candles problem, you need to complete the function birthdayCakeCandles that has an integer array passing as a parameter and need to return the number of values that are maximum.

HackerRank Birthday Cake Candles solution


Problem solution in Python programming.

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the birthdayCakeCandles function below.
def birthdayCakeCandles(ar):
    c = 0
    temp = ar[0]
    for i in range(1,len(ar)):
        if ar[i] > temp:
            temp = ar[i]
    for i in range(0,len(ar)):
        if ar[i] == temp:
            c = c + 1
    return c
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    ar_count = int(input())

    ar = list(map(int, input().rstrip().split()))

    result = birthdayCakeCandles(ar)

    fptr.write(str(result) + '\n')

    fptr.close()




Problem solution in Java Programming.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        int n = in.nextInt();
        int max = 0;
        int sum = 0;
        int num;
        for(int i =0; i < n; i++){
            num = in.nextInt();
            if(num > max){
                sum = 1;
                max = num;
            }else if(num == max){
                sum++;
            }
        }
        System.out.println(sum);
    }
}


Problem solution in C++ programming.

#include <iostream>

using namespace std;

int main() {
	
	int n, maks = 0, kolko, x;
	
	cin >> n;
	
	for( int i=0; i<n; i++ ) {
		cin >> x;
		
		if( x > maks ) {
			maks = x; 
			kolko = 1;
		}
		else if( x == maks )
			kolko++;
	}
	
	cout << kolko;
	
	
	return 0;
}


Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() 
{
    int n;
    scanf("%d", &n);
    
    int h, hm, c;
    c = 0; hm = 0;
    
    for (int idx = 0; idx < n; idx++)
    {
        scanf("%d", &h);
        
        if (h > hm)
        {
            hm = h;
            c = 1;
        }
        else if (h == hm)
        {
            c++;
        }
    }
    printf("%d", c);
    
    return 0;
}


Problem solution in JavaScript programming.

function processData(input) {
    var inputs = (input.split("\n"));
    var n = parseInt(inputs[0]);
    var str_candles = inputs[1].split(" ");
    candles = []
    for (var i=0; i<n; i++) {
        candles.push(parseInt(str_candles[i]));
    }
    var greatest = Number.NEGATIVE_INFINITY;
    var count = 0;
    for (var i=0; i<n; i++) {
        if (candles[i] > greatest) {
            greatest = candles[i];
        }
    }
    for (var i=0; i<n; i++) {
        if (candles[i] == greatest) {
            count += 1;
        }
    }
    console.log(count);
} 
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});