Python interview question: two sum in a sorted array
Problem
prices is a category’s price list, sorted ascending, and at most one pair of different items adds up to exactly target. Return that pair’s indices [i, j] with i < j, or [-1, -1] if there’s none. Use O(1) extra memory, without a price dictionary.
def pair_in_sorted(prices: list[int], target: int) -> list[int]:
Example
prices = [140, 270, 380, 490, 660, 910], target = 760 → [1, 3]270 + 490, indices from 0; 380 can’t pair with itself
Solve it free
Checked against hidden tests, right in your browser. No sign-up needed.