New to computer science and am attempting to verify if there are any non-quadratic approaches (i.e. better performance than Big-O of n^2) for finding a potential specific difference between either adjacent or nonadjacent elements in a sorted array of integers?
For example, if you have an array that's already been sorted, like so:
[1, 4, 6, 6, 10, 12, 13, 15, 16]
And you want to find if there's a difference between any of these elements that equals
3, the pairs here would be
4, 1 and
15, 12 and
But is there an algorithmic approach that would allow me to find these pairs without using two for loops?