Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerRank Birthday Cake Candles problem solution

YASH PAL, 31 July 20244 May 2025

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);
});

Other problems solutions

  • HackerRank Time conversion problem solution
  • HackerRank Grading students problem solution
  • HackerRank Apple and orange problem solution
  • HackerRank Number Line Jumps problem solution
  • HackerRank Between two sets problem solution
algorithm coding problems AlgorithmsHackerRank

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes