We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

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.

## Minimum Absolute Difference in an Array

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.