Monday, September 7, 2020

Hackerrank :- Birthday Cake Candles solution in c#

 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

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

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

 // Complete the birthdayCakeCandles function below.
    static int birthdayCakeCandles(int[] arr) {

 var maxNum = 0;
            var i  = 0;
            var list = new Dictionary<int, int>();
            while (arr.Length > i)
            {
                if (i == 0)
                {
                    maxNum = arr[i];
                    list.Add(maxNum, 1);
                }
                else
                {
                     if(arr[i] > maxNum)
                        maxNum = arr[i];
                    if (list.ContainsKey(arr[i]))
                    {
                        list[arr[i]] = list[arr[i]] + 1;
                    }
                    else
                        list.Add(arr[i], 1);
                }
                i++;
            }

            return list[maxNum];
    }



No comments:

Post a Comment

Kafka setup in window

Here's a step-by-step guide on how to do it : 1. Prerequisites : Before you begin, make sure you have the following prerequisites inst...