Created
December 21, 2017 22:35
-
-
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
This file contains hidden or 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
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