Skip to content

Instantly share code, notes, and snippets.

@ashuorg
Created August 4, 2020 05:32
Show Gist options
  • Save ashuorg/c7efed82079f2d47b30ae56cf1aa8701 to your computer and use it in GitHub Desktop.
Save ashuorg/c7efed82079f2d47b30ae56cf1aa8701 to your computer and use it in GitHub Desktop.
function threeNumberSum(array, targetSum) {
const arr = array.sort(function(a, b){
return a - b;
});
const arrLen = array.length;
const threeNumSum = [];
console.log(array, targetSum);
for (let i=0; i < arrLen - 2; i += 1) {
for (let j=i+1; j < arrLen - 1; j += 1) {
// find third
for (let k=j+1; k < arrLen; k += 1) {
const rem = targetSum - (arr[i] + arr[j]);
if (rem === arr[k]) {
threeNumSum.push([ arr[i], arr[j], arr[k] ]);
}
}
}
}
return threeNumSum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment