Skip to content
  • Home
  • Contact Us
  • About Us
  • Privacy Policy
  • DMCA
  • Linkedin
  • Pinterest
  • Facebook
thecscience

TheCScience

TheCScience is a blog that publishes daily tutorials and guides on engineering subjects and everything that related to computer science and technology

  • Home
  • Human values
  • Microprocessor
  • Digital communication
  • Linux
  • outsystems guide
  • Toggle search form
HackerRank Birthday Cake Candles Problem Solution

HackerRank Birthday Cake Candles Solution

Posted on April 11, 2023April 11, 2023 By Yashwant Parihar No Comments on HackerRank Birthday Cake Candles Solution

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

Table of Contents

  • Birthday Cake Candles C Solution
  • Birthday Cake Candles C++ Solution
  • Birthday Cake Candles C Sharp Solution
  • Birthday Cake Candles Java Solution
  • Birthday Cake Candles JavaScript Solution
  • Birthday Cake Candles Python 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 Tags:C, cpp, CSharp, Hackerrank Solutions, java, javascript, python

Post navigation

Previous Post: HackerRank Mini-Max Sum Problem Solution
Next Post: HackerRank Time Conversion Problem Solution

Related Posts

HackerRank Circular Palindromes Problem Solution HackerRank Circular Palindromes Problem Solution c
HackerRank Jeanie's Route Problem Solution HackerRank Jeanie’s Route Problem Solution c
HackerRank Counting Sort 1 Problem Solution HackerRank Counting Sort 1 Problem Solution c
HackerRank Bonetrousle Problem Solution HackerRank Bonetrousle Problem Solution c
HackerRank String Function Calculation Problem Solution HackerRank String Function Calculation Solution c
HackerRank Picking Numbers Problem Solution HackerRank Picking Numbers Problem Solution c

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Pick Your Subject
Human Values

Copyright © 2023 TheCScience.

Powered by PressBook Grid Blogs theme