Skip to content

Instantly share code, notes, and snippets.

@crullian
Created October 26, 2014 22:22
Show Gist options
  • Save crullian/303455bc8bd33d65f188 to your computer and use it in GitHub Desktop.
Save crullian/303455bc8bd33d65f188 to your computer and use it in GitHub Desktop.
ArrayAdditionI
function ArrayAdditionI(arr) {
arr.sort(function(a, b) {
return a - b;
});
var largest = arr[arr.length - 1]; // Set largest to last (largest) array value
for (var i = 0; i < arr.length; i++) { // Start outer loop
var sum = arr[i];
for (var j = 0; j < arr.length; j++) { // Start inner to begin sum
if (i != j) { // Ensure we don't add the same array index to itself
sum += arr[j];
console.log(sum);
if (sum === largest) {
return true;
}
}
}
for (j = 0; j < arr.length; j++) { // If no match, start 2nd loop to re-iterate removing index values
if (i != j) {
sum -= arr[j];
console.log(sum);
if (sum === largest) {
return true;
}
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment