Created
August 4, 2020 05:32
-
-
Save ashuorg/c7efed82079f2d47b30ae56cf1aa8701 to your computer and use it in GitHub Desktop.
This file contains 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
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