Skip to content

Instantly share code, notes, and snippets.

@pauloportella
Created August 22, 2018 16:22
Show Gist options
  • Save pauloportella/98357abefed94ef0fc13cf77b82840ed to your computer and use it in GitHub Desktop.
Save pauloportella/98357abefed94ef0fc13cf77b82840ed to your computer and use it in GitHub Desktop.
sumOfTwo = (a, b, v) => (
b = new Set(b),
a.some(e => b.has(v - e))
)

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.

Input/Output

[execution time limit] 4 seconds (js)

[input] array.integer a

An array of integers.

Guaranteed constraints: 0 ≤ a.length ≤ 105, -109 ≤ a[i] ≤ 109.

[input] array.integer b

An array of integers.

Guaranteed constraints: 0 ≤ b.length ≤ 105, -109 ≤ b[i] ≤ 109.

[input] integer v

Guaranteed constraints: -109 ≤ v ≤ 109.

[output] boolean

true if there are two elements from a and b which add up to v, and false otherwise.

[JavaScript (ES6)] Syntax Tips

// Prints help message to the console // Returns a string function helloWorld(name) { console.log("This prints to the console when you Run Tests"); return "Hello, " + name; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment