Created
April 17, 2022 06:05
-
-
Save MohamedGamil/11a0549886d9a77b0c3a34e2532261b4 to your computer and use it in GitHub Desktop.
HackerRank / Birthday Cake Candles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const fs = require('fs'); | |
process.stdin.resume(); | |
process.stdin.setEncoding('utf-8'); | |
let inputString = ''; | |
let currentLine = 0; | |
process.stdin.on('data', function(inputStdin) { | |
inputString += inputStdin; | |
}); | |
process.stdin.on('end', function() { | |
inputString = inputString.split('\n'); | |
main(); | |
}); | |
function readLine() { | |
return inputString[currentLine++]; | |
} | |
/* | |
* Complete the 'birthdayCakeCandles' function below. | |
* | |
* The function is expected to return an INTEGER. | |
* The function accepts INTEGER_ARRAY candles as parameter. | |
*/ | |
function birthdayCakeCandles(candles) { | |
const sorted = candles.sort((x, y) => y - x); | |
const tallest = sorted[0]; | |
let count = 0; | |
for (let height of candles) { | |
if (height >= tallest) { | |
count++; | |
} | |
} | |
return count; | |
} | |
function main() { | |
const ws = fs.createWriteStream(process.env.OUTPUT_PATH); | |
const candlesCount = parseInt(readLine().trim(), 10); | |
const candles = readLine().replace(/\s+$/g, '').split(' ').map(candlesTemp => parseInt(candlesTemp, 10)); | |
const result = birthdayCakeCandles(candles); | |
ws.write(result + '\n'); | |
ws.end(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment