Skip to content

Instantly share code, notes, and snippets.

@joshbedo
Created July 9, 2015 13:42
Show Gist options
  • Save joshbedo/5ae51fff0b9377090a26 to your computer and use it in GitHub Desktop.
Save joshbedo/5ae51fff0b9377090a26 to your computer and use it in GitHub Desktop.
3 Sum problem - Given N integers how many triples sum to exactly zero
var numbers = [20,-20, 0, 50, -50, 0];
var len = numbers.length;
var count = 0;
var threeSum = function() {
for(var i = 0;i < len; i++) {
for(var j = i + 1;j < len; j++) {
for(var k = j + 1;k < len; k++) {
if (numbers[i] + numbers[j] + numbers[k] == 0) {
console.log('combination: ', numbers[i], numbers[j], numbers[k]);
count++;
}
}
}
}
return count;
}
console.log('total: ', threeSum());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment