Skip to content
thecscience
THECSICENCE

Learn everything about computer science

  • Home
  • Human values
  • NCERT Solutions
  • HackerRank solutions
    • HackerRank Algorithms problems solutions
    • HackerRank C solutions
    • HackerRank C++ solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
thecscience
THECSICENCE

Learn everything about computer science

HackerRank Birthday Cake Candles Solution

Yashwant Parihar, April 11, 2023April 11, 2023

In this post, We are going to solve HackerRank Birthday Cake Candles Problem. 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 of candles is 4 units high. There are 2 of them, so return 2.

Function Description

Complete the function birthdayCakeCandles in the editor below.

birthday cake candles have the following parameter(s):

  • int candles[n]: the candle heights

Returns

  • int: the number of candles that are the 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 describe the height of candles[I].

Constraints

  • 1 < n < 10 power of 5
  • 1 < candles[I] < 10 power of 7

Sample Input 0

4
3 2 1 3

Sample Output 0

2

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 Problem Solution
HackerRank Birthday Cake Candles Problem Solution

Birthday Cake Candles C Solution

//Author: Koppu
#include <stdio.h>
int calc_candles(int *a, int n) {
	int i;
	int count = 1;
	int max = a[0];
	for(i = 1; i < n; i++) {
		if(a[i] > max) {
			max = a[i];
			count = 1;
		}
		else if(a[i] == max) {
			count++;
		}
		else
			continue;
	}
	return count;
}

int main(void) {
	int i;
	int n;
	scanf("%d", &n);
	int a[n];
	for(i = 0; i < n; i++) {
		scanf("%d", &a[i]);
	}
	printf("%d",calc_candles(a, n));
	return 0;
}

Birthday Cake Candles C++ Solution

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


int main() {
    
    int n;
    cin>>n;
    int mx = -1;
    int count = 0;
    for(int i =0; i<n; i++) {
        int a;
        cin>>a;
        if(a>mx) {
            mx = a;
            count = 1;
        } else if( a == mx ) {
            count++;
        }
    }
    cout<<count<<"\n";
    return 0;
}

Birthday Cake Candles C Sharp Solution

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {

    static void Main(String[] args) {
        int n = Convert.ToInt32(Console.ReadLine());
        string[] height_temp = Console.ReadLine().Split(' ');
        int[] height = Array.ConvertAll(height_temp,Int32.Parse);
        
        int blown = 0;
        int maxHeight = 0;
        for (int i=0; i<height.Count(); i++)
        {
            if (height[i]>maxHeight)
            {
               maxHeight = height[i];
                blown = 1;
            }
            else if (height[i]==maxHeight)
            {
                blown++;
            }
        }
        Console.WriteLine(blown);
    }
}

Birthday Cake Candles Java Solution

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] heights = new int[n];
        for (int i = 0; i < n; i++) {
            heights[i] = scanner.nextInt();
        }
        int maxHeight = 0;
        int count = 0;
        for (int height : heights) {
            if (height > maxHeight) {
                maxHeight = height;
                count = 1;
            } else if (height == maxHeight) {
                count++;
            }
        }
        System.out.println(count);
    }
}

Birthday Cake Candles JavaScript Solution

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function main() {
    var n = parseInt(readLine());
    height = readLine().split(' ');
    height = height.map(Number);
    console.log(numOfTallest(height));

}

function numOfTallest(arr){
	var maxVal = 0;
	var count = 0;
	arr.map(
		function(a){
			if(a>maxVal){
				count = 1;
				maxVal = a;
			}	else if(a===maxVal){
				count++;
			}
		}
	);
	return count;
}

Birthday Cake Candles Python Solution

#!/bin/python3

import sys


n = int(input().strip())
height = [int(height_temp) for height_temp in input().strip().split(' ')]
height.sort()
print (height.count(height[-1]))

Other Solutions

  • HackerRank Time Conversion Problem Solution
  • HackerRank Grading Students Problem Solution
c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython

Post navigation

Previous post
Next post

Leave a Reply

You must be logged in to post a comment.

  • HackerRank Dynamic Array Problem Solution
  • HackerRank 2D Array – DS Problem Solution
  • Hackerrank Array – DS Problem Solution
  • Von Neumann and Harvard Machine Architecture
  • Development of Computers
©2025 THECSICENCE | WordPress Theme by SuperbThemes