Created
August 28, 2018 12:53
-
-
Save ronit-mukherjee/7e78abf4a7e493b1db990d8e66eee59b to your computer and use it in GitHub Desktop.
HackerRank - Mini-Max sum (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/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