Skip to content

Instantly share code, notes, and snippets.

@tanyapowell
Created October 20, 2022 20:55
Show Gist options
  • Select an option

  • Save tanyapowell/b9a772bfb3210d9ed15f5726cea03d09 to your computer and use it in GitHub Desktop.

Select an option

Save tanyapowell/b9a772bfb3210d9ed15f5726cea03d09 to your computer and use it in GitHub Desktop.
Hackerrank solution for Thomas cohort
'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 'breakingRecords' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts INTEGER_ARRAY scores as parameter.
*/
function breakingRecords(scores) {
const removedDuplicates = [...new Set(scores)];
let currentLowestNumber = scores[0];
let currentHighestNumber = scores[0];
let howManyTimesHasTheHighestNumberBeenChanged = 0;
let lowestNumberChangesCounter = 0;
for (let i = 0; i < removedDuplicates.length; i++) {
if(removedDuplicates[i] > currentHighestNumber) {
currentHighestNumber = removedDuplicates[i];
howManyTimesHasTheHighestNumberBeenChanged++;
}
if(removedDuplicates[i] < currentLowestNumber) {
currentLowestNumber = removedDuplicates[i];
lowestNumberChangesCounter++;
}
}
return [howManyTimesHasTheHighestNumberBeenChanged, lowestNumberChangesCounter];
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const n = parseInt(readLine().trim(), 10);
const scores = readLine().replace(/\s+$/g, '').split(' ').map(scoresTemp => parseInt(scoresTemp, 10));
const result = breakingRecords(scores);
ws.write(result.join(' ') + '\n');
ws.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment