Skip to content

Instantly share code, notes, and snippets.

@farskid
Created December 21, 2017 22:35
Show Gist options
  • Save farskid/008088d7123ffc1c0344fbb62659ef35 to your computer and use it in GitHub Desktop.
Save farskid/008088d7123ffc1c0344fbb62659ef35 to your computer and use it in GitHub Desktop.
Minimum and Maximum values that can be calculated by summing exactly `n-1` of the `n` integers
function maxMinSum(intArray) {
var sum = sumOfArray(intArray);
var sums = intArray.map(num => sum - num);
return [minOfArray(sums), maxOfArray(sums)].join(' ');
}
function sumOfArray(intArray) {
return intArray.reduce((total, curr) => total += curr, 0);
}
function maxOfArray(intArray) {
return Math.max.apply(null, intArray);
}
function minOfArray(intArray) {
return Math.min.apply(null, intArray);
}
console.log(maxMinSum([1,2,3,4,5]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment