Let A[1]<=A[2]<=….<=A[n]. Let X be an arbitrary number. Give an algorithm find all pairs of A[i] and A[j] such that A[j] – A[i] >= X. All numbers are positive integers.
If you want to see the original problem. Here it is:
Let P = {p1; p2; ; pn} be a set of n points in a 2-dimensional space, where pi = (xi; yi) for each i. Let D = (dx; dy). The problem is to decide whether there exists a pair of points pi and pj such that xj – xi >= dx and yj – yi >= dy. You can easily solve this problem in O(n^2) time by considering all possible pairs of points. But we are interested in developing an O(n log n) time algorithm.
Since the input is sorted, you could binary search for
X - A[i]for eachifrom 1 to n. Since items can be equal, you’d need to find the upper and lower bounds for the binary search though. I think this would be O(nlogn)…