Skip to content

Instantly share code, notes, and snippets.

@Nicknyr
Created July 12, 2020 23:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nicknyr/7c7bd7fcd31bf0c415fe36e5d808246c to your computer and use it in GitHub Desktop.
Save Nicknyr/7c7bd7fcd31bf0c415fe36e5d808246c to your computer and use it in GitHub Desktop.
CodeSignal - sumOfTwo
/*
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