HackerRank Birthday Cake Candles problem solution

In this HackerRank Birthday Cake Candles problem solution, You are in charge of the cake for a child’s birthday. You have decided the cake will have one candle for each year of their total age. They will only be able to blow out the tallest of the candles. Count how many candles are tallest.

Example

candles = [4,4,1,3]

The maximum height candles are 4 units high. There are 2 of them, so return 2.

Function Description

Complete the function birthdayCakeCandles in the editor below.

birthdayCakeCandles has the following parameter(s):

int candles[n]: the candle heights

Returns

int: the number of candles that are tallest

Input Format

The first line contains a single integer, n, the size of candles[].

The second line contains n space-separated integers, where each integer i describes the height of candles[i].

Constraints

1 <= n <= 10^5

1 <= candles[i] <= 10^7

hackerrank birthday cake candles solution
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);
});