Skip to content

Instantly share code, notes, and snippets.

@robpe
Last active January 3, 2016 13:19
Show Gist options
  • Save robpe/8468777 to your computer and use it in GitHub Desktop.
Save robpe/8468777 to your computer and use it in GitHub Desktop.
/* https://github.com/fay-jai/coderbyte */
function ArrayAdditionI(arr) {
var arr = arr.sort(function(a, b) { return a-b; })
max = arr.pop(),
sums = [];
(function smash(sum, arr) {
if(arr.length == 0 || sum > max ) return;
sums.push(sum)
for(var i in arr) {
copy = arr.slice()
smash(sum+copy.splice(i, 1)[0], copy)
}
})(0, arr)
return sums.indexOf(max) > -1
}
/* test cases */
console.log(
ArrayAdditionI([4, 6, 23, 10, 1, 3]) == true,
ArrayAdditionI([3, 5, -1, 8, 12]) == true,
ArrayAdditionI([1, 2, 3, 100]) == false
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment