Cross-Array Pair Sum Check on Two Ordered Lists
Arrays & HashingAuto-graded · 21 test casesLast updated
Decide whether one value from each of two sorted lists adds up to a target, using a linear-time opposite-direction two-pointer scan with constant extra space.
You are given two lists of integers, first and second, each sorted in non-decreasing order, and an integer goal.
Determine whether it is possible to choose exactly one value from first and exactly one value from second so that the two chosen values add up to goal.
Both values must come from different lists: two elements taken from the same list never count as a valid pair. Duplicate values are allowed in either list, and a value may be used as long as it appears in its own list.
Task
Implement pair_sum_across_lists(first, second, goal), which returns True if such a pair exists and False otherwise.
If either list is empty, no pair can be formed, so the answer is False.
Aim for a solution that runs in time proportional to len(first) + len(second) and uses only constant extra memory. Your comparison logic should also be conceptually safe for 64-bit signed integer ranges, meaning it should not rely on intermediate values that would fall outside that range in a fixed-width language. (Python integers do not overflow, so a direct sum is fine in Python.)
Input
first: a list of integers sorted in non-decreasing order.second: a list of integers sorted in non-decreasing order.goal: an integer.
Output
- A boolean:
Trueif someainfirstand somebinsecondsatisfya + b == goal, otherwiseFalse.
Constraints
0 <= len(first), len(second) <= 10^6-2^63 <= first[i], second[j], goal <= 2^63 - 1- Both lists are sorted in non-decreasing order.
- Target complexity:
O(len(first) + len(second))time andO(1)extra space.
Examples
Example 1
Input: pair_sum_across_lists([1, 3, 5, 8], [2, 4, 9, 11], 14)
Output: True
Example 2
Input: pair_sum_across_lists([-10, -3, 0, 4], [-5, 1, 7], -15)
Output: True
Example 3
Input: pair_sum_across_lists([1, 2, 3], [4, 5, 6], 20)
Output: False
Hints
Hint 1
Think about the classic two-sum on a single sorted array, where one pointer starts at each end. Here the two ends belong to different lists.
Hint 2
Start at the smallest element of the first list and the largest element of the second list. If their sum is too small, which pointer can move to raise it? If too large, which can lower it?
Hint 3
Each step permanently discards one element that cannot be part of any valid pair, so the scan ends after at most len(first) + len(second) steps. Empty lists make the loop never execute.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Place one pointer i at the start of first (its smallest value) and another pointer j at the end of second (its largest value). Repeat while both pointers are inside their lists:
- Compute
first[i] + second[j]. - If it equals
goal, returnTrue. - If it is smaller than
goal, advancei. Moving to a larger element offirstis the only way to increase the sum. - If it is larger than
goal, decrementj. Moving to a smaller element ofsecondis the only way to decrease the sum.
If a pointer leaves its list, return False.
Why it is correct
Invariant: if a valid pair at positions (p, q) exists, then i <= p and q <= j.
- If the sum is too small, then
first[i] + second[j] < goal. For everyq <= j,first[i] + second[q] <= first[i] + second[j] < goal, sofirst[i]cannot pair with any remaining element ofsecond. Discardingiis safe. - If the sum is too large, then for every
p >= i,first[p] + second[j] >= first[i] + second[j] > goal, sosecond[j]cannot pair with any remaining element offirst. Discardingjis safe.
Since no valid pair is ever discarded, the scan finds one if it exists. Duplicates need no special handling because the argument relies only on non-strict ordering. An empty list makes the loop condition fail immediately, giving False.
Overflow note
Python integers have arbitrary precision, so the direct sum is safe. In C++ or Java with 64-bit integers, you would compare a + b against goal using a sign case analysis (for example, for two positives check a > MAX - b) so no intermediate value leaves the int64 range.
Complexity
- Time:
O(m + n), since each iteration moves exactly one pointer monotonically. - Space:
O(1).
Reference solution
from typing import List
def pair_sum_across_lists(first: List[int], second: List[int], goal: int) -> bool:
i, j = 0, len(second) - 1
while i < len(first) and j >= 0:
total = first[i] + second[j]
if total == goal:
return True
if total < goal:
i += 1
else:
j -= 1
return False