You are viewing a single comment's thread. Return to all comments →
Why should we consider only consecutive pairs? It can be any two elements from the array right?
As the array has been sorted, so we need to consider the difference between only the consecutive elements. In a sorted array, the difference will increase if you skip elements to calculate the difference.
The values that are closest to one another are the ones that will have the least difference.
After sorting I know that for any value, i , the values at i-1 and i+1 are
closest to i.Therefore, I dont have to consider all pairs that contain, i, I can just consider the ones that are going to yield the least difference, i's neighbors. Everyone else is farther away.