Created
July 12, 2020 23:24
-
-
Save Nicknyr/7c7bd7fcd31bf0c415fe36e5d808246c to your computer and use it in GitHub Desktop.
CodeSignal - sumOfTwo
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
/* | |
You have two integer arrays, a and b, and an integer target value v. Determine whether there is a pair of numbers, where one number is taken from a and the other from b, that can be added together to get a sum of v. Return true if such a pair exists, otherwise return false. | |
Example | |
For a = [1, 2, 3], b = [10, 20, 30, 40], and v = 42, the output should be | |
sumOfTwo(a, b, v) = true. | |
*/ | |
function sumOfTwo(a, b, v) { | |
let bSet = new Set(b); | |
for(let i = 0; i < a.length; i++) { | |
if(bSet.has(v - a[i])) { | |
return true; | |
} | |
} | |
return false; | |
} | |
/* | |
Solves but takes too long due to nested for loops | |
for(let i = 0; i < a.length; i++) { | |
for(let j = 0; j < b.length; j++) { | |
if(a[i] + b[j] === v) { | |
return true; | |
} | |
} | |
} | |
return false; | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment