Birthday Cake Candles HackerRank Solution in C, C++, Java, Python

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

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.

 

Birthday Cake Candles HackerRank Solution in C

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    long int i,n,current,tallest,nbr;
    tallest=0;
    nbr=0;
    scanf("%ld",&n);
    for(i=0;i<n;i++){
        scanf("%ld",&current);
        if(tallest<current){
            tallest=current;
            nbr=1;
        }else if (tallest==current){
            nbr++;
        }
    }
    printf("%d",nbr);
    return 0;
}

Birthday Cake Candles HackerRank Solution in C++

#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;
    return 0;
}

Birthday Cake Candles HackerRank Solution in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] candles = new int[n];
        int max=-1;
        int count=0;
        for(int n0=0; n0<n; n0++){
            candles[n0]= sc.nextInt();
            if(candles[n0]>max){
                max = candles[n0];
                count = 1;
            }
            else if(candles[n0]==max){
                count++;
            }
        }
        System.out.println(count);
    }
}

Birthday Cake Candles HackerRank Solution in Python

n = raw_input()

l = map(int, raw_input().strip().split(' '))
print l.count(max(l))

Birthday Cake Candles HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
    static void Main(String[] args) {
        Console.ReadLine();
        var heights = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        var max = heights.Max();
        var count = heights.Count(it => it == max);
        Console.WriteLine(count);
    }
}

Attempt – Birthday Cake Candles HackerRank Challenge

Link – https://www.hackerrank.com/challenges/birthday-cake-candles

Next Challenge HackerRank Solution

Link – https://exploringbits.com/time-conversion-hackerrank-solution/

Leave a Comment