Interview Question

Software Engineer Interview



this is just a two-sum problem. given a sorted array and a number X, find all pairs whose sum is X in a efficient way.


Interview Answers

2 Answers


To solve this problem, you need to iterate through the array from index zero using i and another index j iterating through the subarray in the right of the current index of i.At each instance of iteration, add the element at i and at j.If the sum is X add the two pairs in the list of pairs to be returned.After exhausting the array, return the list of ordered pairs.

Paul Odero - Nairobi on


i iterates from 0 to n-2; for every range[i+1,n-1], do binary search for target-num[i], if found, push to the result. remember to remove duplicate sum candidates by using continue and break statements.

brady on

Add Answers or Comments

To comment on this, Sign In or Sign Up.