HackerRank Birthday Cake Candles Solution

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

HackerRank Birthday Cake Candles Solution
HackerRank Birthday Cake Candles Solutions

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank Birthday Cake Candles Solution

Problem

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.

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 <= 105
  • 1 <= candles[i] <= 107

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 Solutions

Birthday Cake Candles Solution in C

#include<stdio.h>
#include<stdlib.h>
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;
}

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++;
        cout<<count<<endl;
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    return 0;
}

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(){
		try{
			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){
					ans++;	
				}
			}
			System.out.println(ans);
			
		}
		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();
	}
}
Ezoicreport this ad

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

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
    console.log(numCoincidences);
} 
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});
process.stdin.on("end", function () {
   processData(_input);
});

Birthday Cake Candles Solution in Scala

object Solution {
    def main(args: Array[String]) {
      readLine
      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)
      }    
    }
}

Birthday Cake Candles Solution in Pascal

(* Enter your code here. Read input from STDIN. Print output to STDOUT *)
//birthdaycakes
const fi='';
fo='';
var f:text;
    n,i,max,res,v:longint;
    a:array[1..100000]of longint;
begin
 max:=0;res:=0;
    assign(f,fi);
    reset(f);
         readln(f,n);
         for i:=1 to n do
         begin
             read(f,a[i]);
             if a[i]>max then max:=a[i];
         end;
    close(f);
       for i:=1 to n do
       begin
           if a[i]=max then inc(res);
       end;
    assign(f,fo);
    rewrite(f);
             writeln(f,res);
    close(f);
end.

Disclaimer: This problem (Birthday Cake Candles) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: HackerRank Diagonal Difference Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad