Find a pair summing to a target, in a sorted array

easy~15 min#arrays#two-pointers

You are given an array of integers sorted in non-decreasing order, and a target value. Return the indices of two distinct elements that sum to the target, or an empty result if no such pair exists.

Solve it in O(n) time and O(1) extra space.

Then answer the part that is actually being assessed: why can you safely discard a candidate when you move a pointer? An interviewer who hears "I move left forward if the sum is too small" and nothing else has learned that you memorised the shape.

Solution — locked

Sign in to unlock this one

The solution opens once you explain the idea in your own words and it passes the grader — which needs an account to record. Signing in is free.

Log in