Created
March 17, 2022 07:36
-
-
Save neos1803/2cd8e4d979d0f977914e2e036a594d09 to your computer and use it in GitHub Desktop.
Function to return the minimum sum and maximum sum of an array
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
function minMaxSum(array = []) { | |
const sorted = array.sort((a, b) => a - b) // Ascended sort | |
const minSum = sorted.slice(0, array.length/2).reduce((p, c) => p+=c); | |
const maxSum = sorted.slice(array.length/2, array.length).reduce((p, c) => p+=c); | |
return [minSum, maxSum] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment