Created
August 28, 2018 14:10
-
-
Save ronit-mukherjee/fbab6ca286a0beecf2367a6fcdf2672f to your computer and use it in GitHub Desktop.
HackerRank - Bithday Cake Candle (JS)
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
//https://www.hackerrank.com/challenges/birthday-cake-candles/problem | |
'use strict'; | |
const fs = require('fs'); | |
process.stdin.resume(); | |
process.stdin.setEncoding('utf-8'); | |
let inputString = ''; | |
let currentLine = 0; | |
process.stdin.on('data', inputStdin => { | |
inputString += inputStdin; | |
}); | |
process.stdin.on('end', _ => { | |
inputString = inputString.replace(/\s*$/, '') | |
.split('\n') | |
.map(str => str.replace(/\s*$/, '')); | |
main(); | |
}); | |
function readLine() { | |
return inputString[currentLine++]; | |
} | |
// Complete the birthdayCakeCandles function below. | |
function birthdayCakeCandles(ar) { | |
let blownCandleCount = 0; | |
let max = 0; | |
for(let i=0; i<ar.length; i++){ | |
if(ar[i]> max){ | |
max = ar[i]; | |
blownCandleCount = 1; | |
} else{ | |
if(max==ar[i]){ | |
blownCandleCount++; | |
} | |
} | |
} | |
return blownCandleCount; | |
} | |
function main() { | |
const ws = fs.createWriteStream(process.env.OUTPUT_PATH); | |
const arCount = parseInt(readLine(), 10); | |
const ar = readLine().split(' ').map(arTemp => parseInt(arTemp, 10)); | |
let result = birthdayCakeCandles(ar); | |
ws.write(result + "\n"); | |
ws.end(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment