Birthday cake candle Hackerrank solution in C , C++ , python and Java



Problem statement :  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,3,1 ]

The maximum height candles are  units high. There are  of them, so return .

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, , the size of .
The second line contains  space-separated integers, where each integer  describes the height of .

Constraints

  •  1⩽n ≤ 10⁵
  • 1≤ candles[i]≤10⁷

Sample Input 0

4
3 2 1 3

Sample Output 0

2

Explanation 0

Candle heights are . The tallest candles are  units, and there are  of them.  

Solution : 

Code in C :

#include<stdio.h>
int main()
{
    int n,i;
    scanf("%d",&n);
    int max=0,j=0cand[n];
    for ( i = 0i < ni++)
    {
        scanf("%d",&cand[i]); 
        if ( cand[i] >= max )
        {
            max=cand[i];
        }
        
    }
    
    for ( i = 0i < ni++)
    {
        if ( cand[i] == max )
        {
            j++;
        }     
    }
    
     printf("%d",j); 
}

Code in C++ :

#include<iostream>
using namespace std ;
int main()
{
    int n,i;
    cin>>n;
    int max=0,j=0cand[n];
    for ( i = 0i < ni++)
    {
        cin>>cand[i];
        if ( cand[i] >= max )
        {
            max=cand[i];
        }
        
    }
    
    for ( i = 0i < ni++)
    {
        if ( cand[i] == max )
        {
            j++;
        }     
    }
    
    cout<<j<<endl;
}

Code in python :

Code in Java :

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();
    }
}

Please comment if you have any doubt or for any other problem .
Thanks