Created
September 1, 2017 16:54
-
-
Save cassiocardoso/d77c7917a1969a99b5766b0023d7d6f3 to your computer and use it in GitHub Desktop.
Unique triples problem
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
const array = [-1, 0, 1, 2, -1, -4]; | |
const length = array.length; | |
const sum = 0; | |
const solutions = []; | |
array.sort(); | |
array.map((d, i) => { | |
let j = i + 1; | |
let k = length - 1; | |
while (j < k) { | |
if (array[i] + array[j] + array[k] === sum) { | |
solutions.push([array[i], array[j], array[k]]); | |
j++; | |
k--; | |
} else { | |
j++; | |
} | |
} | |
}); | |
console.log('solution', solutions); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment