Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

GATE | GATE MOCK 2017 | Question 23

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Consider a sorted array of n numbers. What would be the time complexity of the best known algorithm to find a pair ‘a’ and ‘b’ such that |a-b| = k , k being a positive integer.
(A) O(n)
(B) O(n log n)
(C) O(n ^ 2)
(D) O(log n)


Answer: (A)

Explanation: Just maintain two pointers at the start and accordingly increment one of them depending upon whether difference is less than or greater than k. Just a single pass is required so the answer is O(n).

Quiz of this Question

My Personal Notes arrow_drop_up
Last Updated : 10 Jun, 2019
Like Article
Save Article
Similar Reads