FastPrepTarget Sum Across Two Sorted Arrays

Target Sum Across Two Sorted Arrays

Bloomberg LP logoBloomberg LP● EasyNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem 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) → boolean

Examples

Example 1

first = [1,3,5]second = [2,4,8]target = 9return = true

The values 1 and 8 form the target.

Example 2

first = []second = [1,2,3]target = 4return = false

No pair exists when either array is empty.

Example 3

first = [2147483647]second = [2147483647]target = 4294967294return = true

The 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.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public boolean hasCrossArrayTargetSum(int[] first, int[] second, long target) {
  // write your code here
}
first[1,3,5]
second[2,4,8]
target9
expectedtrue
Checking account…