Friday, March 21, 2014

Find all pairs (x, y) in a sorted array so that x + y < z

Problem
Given a sorted integer array and number z find all pairs (x, y) in the array so that x + y < z. Can it be done better than O(n^2)?
Solution

Method 1
The solution can be found in O(n^2) where we select 1st element from n elements and 2nd element y from n-1 elements, and hence n(n-1)/2 ~ O(n^2)

Reference - stackoverflow

0 comments:

Post a Comment