Skip to content

Instantly share code, notes, and snippets.

@BeejLuig
Last active October 11, 2017 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BeejLuig/14f6f4f1c365976ff9fb69ad05e180ad to your computer and use it in GitHub Desktop.
Save BeejLuig/14f6f4f1c365976ff9fb69ad05e180ad to your computer and use it in GitHub Desktop.
Zero Balanced Array
/*
* 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