CeeJtheDay
CeeJtheDay

Reputation: 11

Birthday cake candles- hackerrank

My code for this problem works on 8/9 test cases. but i cant figure out why it wont work for the last case.

function birthdayCakeCandles(ar) {
    const candleHeights = {};
    for (let i = 0; i < ar.length; i++) {
        if (candleHeights[ar[i]]) {
            candleHeights[ar[i]]++;
        } else {
            candleHeights[ar[i]] = 1;
        }
    }
    let tallestCandle = 0;
    let candleCount;
    for (let [key, value] of Object.entries(candleHeights)) {
    if (key > tallestCandle){
        tallestCandle = key;
        candleCount = value;
        }
    }
    return candleCount;
}

Problem:

You are in charge of the cake for your niece's birthday and have decided the cake will have one candle for each year of her total age. When she blows out the candles, she’ll only be able to blow out the tallest ones. Your task is to find out how many candles she can successfully blow out.

For example, if your niece is turning years old, and the cake will have candles of height , , , , she will be able to blow out candles successfully, since the tallest candles are of height and there are such candles.

Function Description

Complete the function birthdayCakeCandles in the editor below. It must return an integer representing the number of candles she can blow out.

birthdayCakeCandles has the following parameter(s):

  • ar: an array of integers representing candle heights Input Format

The first line contains a single integer, , denoting the number of candles on the cake. The second line contains space-separated integers, where each integer describes the height of candle .

Output Format

Return the number of candles that can be blown out on a new line.

Sample Input

4
3 2 1 3

Sample Output

2

Explanation

We have one candle of height , one candle of height , and two candles of height . Your niece only blows out the tallest candles, meaning the candles where . Because there are such candles, we print on a new line.

constraints

The test case that fails looks like this:

Input (stdin)

100000

1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 10{-truncated-}

Expected Output

99999

All but 1 number in the array is 1000, and one 999.

I'm not sure why this fails, could be the constraints.

Upvotes: 1

Views: 1224

Answers (1)

hamate
hamate

Reputation: 21

This was my first try, I thought it is the best because it looks pretty cool, however it failed on some test cases:

function birthdayCakeCandles(candles) {
  return candles.filter(n => n === Math.max(...candles)).length;
}

Then I tried this and it passes on all test cases:

function birthdayCakeCandles(candles) {
    let max = 0;
    let counter = 0;

    candles.forEach(item => {
        if (item > max) {
            max = item;
            counter = 1;
        } else if (item === max) {
            counter++;
        }
    });
    return counter;
}

Upvotes: 1

Related Questions