Last active
October 11, 2017 02:51
-
-
Save BeejLuig/14f6f4f1c365976ff9fb69ad05e180ad to your computer and use it in GitHub Desktop.
Zero Balanced 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
/* | |
* An array is called zero-balanced if its elements sum to 0 and for each positive element n, | |
* there exists another element that is the negative of n. Write a function named ìsZeroBalanced that returns true | |
* if its argument is zero-balanced array, else return false. Note that an empty array will not sum to zero. | |
*/ | |
function isZeroBalanced(a){ | |
return a.length > 0 && a.every(x => a.includes(-x)) && a.reduce((acc, x) => acc + x) === 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment