Problem · Array
Target Sum Across Two Sorted Arrays
Learn this problemProblem statement
You are given two integer arrays first and second, each sorted in nondecreasing order, and a signed 64-bit integer target.
Return true if you can choose exactly one value from first and exactly one value from second whose sum equals target. Otherwise, return false. An empty input array therefore always produces false. Compute every candidate sum in signed 64-bit arithmetic.
Function
hasCrossArrayTargetSum(first: int[], second: int[], target: long) → booleanExamples
Example 1
first = [1,3,5]second = [2,4,8]target = 9return = trueThe values 1 and 8 form the target.
Example 2
first = []second = [1,2,3]target = 4return = falseNo pair exists when either array is empty.
Example 3
first = [2147483647]second = [2147483647]target = 4294967294return = trueThe pair sum is computed in signed 64-bit arithmetic.
Constraints
0 <= first.length, second.length <= 200000.- Both arrays are sorted in nondecreasing order.
- Every array value is a signed 32-bit integer.
-2^32 <= target <= 2^32 - 2.