Skip to content

Instantly share code, notes, and snippets.

@MohamedGamil
Created April 17, 2022 06:10
Show Gist options
  • Save MohamedGamil/99c13b34a5345fbf02bdab1f5877da5f to your computer and use it in GitHub Desktop.
Save MohamedGamil/99c13b34a5345fbf02bdab1f5877da5f to your computer and use it in GitHub Desktop.
HackerRank / Mini-Max Sum
'use strict';
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 'miniMaxSum' function below.
*
* The function accepts INTEGER_ARRAY arr as parameter.
*/
function miniMaxSum(arr) {
const sorted = arr.sort((x, y) => x - y);
const high = sorted.slice(0, 4);
const low = sorted.reverse().slice(0, 4);
const sum = (nums) => {
let sum_ = 0;
for(let num of nums) {
sum_ += num;
}
return sum_;
};
const str = sum(high) + " " + sum(low);
console.log(str);
}
function main() {
const arr = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
miniMaxSum(arr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment