Interview Question

Software Engineer Interview

-

Google

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.

Answer

Interview Answers

2 Answers

0

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

0

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.