Created
July 9, 2015 13:42
-
-
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
This file contains hidden or 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
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