Skip to content

Instantly share code, notes, and snippets.

@ronit-mukherjee
Created August 28, 2018 12:53
Show Gist options
  • Save ronit-mukherjee/7e78abf4a7e493b1db990d8e66eee59b to your computer and use it in GitHub Desktop.
Save ronit-mukherjee/7e78abf4a7e493b1db990d8e66eee59b to your computer and use it in GitHub Desktop.
HackerRank - Mini-Max sum (JS)
//https://www.hackerrank.com/challenges/mini-max-sum/problem
'use strict';
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 miniMaxSum function below.
function miniMaxSum(arr) {
arr = arr.sort();
let arrLength = 5;
let maxIndex = 4;
let minSum = 0;
let maxSum = 0;
for(let i=0;i<=3;i++){
minSum += arr[i];
maxSum += arr[maxIndex - i]
}
console.log(minSum + " " + maxSum);
}
function main() {
const arr = readLine().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