HackerRank Birthday Cake Candles Solution

Hello Programmers, In this post, you will learn how to solve HackerRank Birthday Cake Candles Solution. This problem is a part of the HackerRank Algorithms Series.

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the HackerRank Algorithms problems using C, CPP, JAVA, PYTHON, JavaScript & SCALA Programming Languages.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

HackerRank Birthday Cake Candles Solution
HackerRank Birthday Cake Candles Solution

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology, Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

HackerRank Birthday Cake Candles


You are in charge of the cake for a childs 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.


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


  • 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].


  • 1 <= n <= 105
  • 1 <= candles[i] <= 107

Sample Input 0

3 2 1 3

Sample Output 0


Explanation 0

Candle heights are [3, 2, 1, 3]. The tallest candles are 3 units, and there are 2 of them.

HackerRank Birthday Cake Candles Solution

HackerRank Birthday Cake Candles Solution in C


int cmpfunc(const void *a, const void *b){
	return (*(int *)a - *(int *)b);
int main(){
	int n; scanf("%d", &n);
	int a[n], i, d, c = 0;
	for(i = 0; i < n; scanf("%d", &a[i++]));
	qsort(a, n, sizeof(int), cmpfunc);
	d = a[n - 1];
	for(i = n - 2; i >= 0; i--)	if(a[i] == d)	c++;
	printf("%d\n", c + 1);
	return 0;

HackerRank Birthday Cake Candles Solution in Cpp

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int N; cin>>N;
    int a[N];
    for(int i=0;i<N;i++) cin>>a[i];
    int biggest = 0;
    for(int i=0;i<N;i++) biggest = max(biggest, a[i]);
    int count = 0;
    for(int i=0;i<N;i++) if (a[i] == biggest) count++;
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    return 0;

HackerRank Birthday Cake Candles Solution in Java

/* Programming Competition - Template (Horatiu Lazu) */

import java.io.*;
import java.util.*;
import java.lang.*;
import java.awt.*;
import java.awt.geom.*;
import java.math.*;
import java.text.*;

class Main{
	BufferedReader in;
	StringTokenizer st;
	public static void main (String [] args){
		new Main();

	public Main(){
			in = new BufferedReader(new InputStreamReader(System.in));
			int N = nextInt();
			int [] arr = new int[N];
			for(int x = 0; x < N; x++)
				arr[x] = nextInt();
			int largestSoFar = 0;
			int ans = 0;
			for(int x = 0; x < N; x++){
				if (arr[x] > largestSoFar){
					largestSoFar = Math.max(arr[x], largestSoFar);
					ans = 0;
				if (arr[x] == largestSoFar){
		catch(IOException e){
			System.out.println("IO: General");
	String next() throws IOException {
		while (st == null || !st.hasMoreTokens())
	   	 	st = new StringTokenizer(in.readLine().trim());
		return st.nextToken();

	long nextLong() throws IOException {
		return Long.parseLong(next());

	int nextInt() throws IOException {
		return Integer.parseInt(next());

	double nextDouble() throws IOException {
		return Double.parseDouble(next());

	String nextLine() throws IOException {
		return in.readLine().trim();

HackerRank Birthday Cake Candles Solution in Python

# Enter your code here. Read input from STDIN. Print output to STDOUT

n = int(raw_input().strip())
H = map(int, raw_input().strip().split(' '))

print H.count(max(H))

HackerRank Birthday Cake Candles Solution using JavaScript

function processData(input) {
    // Define variables
    var inputs = input.split("\n"),
        noCandles = Number(inputs[0]),
        arrCandles = inputs[1].split(" ").map(Number),
        greatestHeight = 0,
        numCoincidences = 0;
    // Run over array to get highest candles
    for(var i = 0; i < noCandles; i++) {
        if( greatestHeight < arrCandles[i] ){
            greatestHeight = arrCandles[i];
            numCoincidences = 1;
        else {
            if(greatestHeight == arrCandles[i])
                numCoincidences += 1;
    // Print answer

_input = "";
process.stdin.on("data", function (input) {
    _input += input;

process.stdin.on("end", function () {

HackerRank Birthday Cake Candles Solution in Scala

object Solution {

    def main(args: Array[String]) {
      val s = readLine
      if (s != null) {
        println(s.split("\\s+").map(x=> x.toInt).groupBy(identity).map(t => (t._1, t._2.length)).maxBy(_._1)._2)

HackerRank Birthday Cake Candles Solution in Pascal

(* Enter your code here. Read input from STDIN. Print output to STDOUT *)
const fi='';
var f:text;
    a:array[1..100000]of longint;
         for i:=1 to n do
             if a[i]>max then max:=a[i];
       for i:=1 to n do

           if a[i]=max then inc(res);

Disclaimer: This problem (Birthday Cake Candles) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.


1. How do you solve the first question in HackerRank?

If you want to solve the first question of Hackerrank then you have to decide which programing language you want to practice i.e C programming, Cpp Programing, or Java programming then you have to start with the first program HELLO WORLD.

2. How do I find my HackerRank ID?

You will receive an email from HackerRank to confirm your access to the ID. Once you have confirmed your email, the entry will show up as verified on the settings page. You will also have an option to “Make primary”. Click on that option. Read more

3. Does HackerRank detect cheating?

yes, HackerRank uses a powerful tool to detect plagiarism in the candidates’ submitted code. The Test report of a candidate highlights any plagiarized portions in the submitted code and helps evaluators to verify the integrity of answers provided in the Test.

4. Does HackerRank use camera?

No for coding practice Hackerrank does not use camera but for companies’ interviews code submission time Hackerrank uses the camera.

5. Should I put HackerRank certificate on resume?

These certificates are useless, and you should not put them on your resume. The experience you gained from getting them is not useless. Use it to build a portfolio, and link to it on your resume. 

6. Can I retake HackerRank test?

The company which sent you the HackerRank Test invite owns your Test submissions and results. It’s their discretion to permit a reattempt for a particular Test. If you wish to retake the test, we recommend that you contact the concerned recruiter who invited you to the Test and request a re-invite. 

7. What is HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedi

Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe. 

Sharing Is Caring

Leave a Comment