Finding the minimum absolute difference among array elements →
Let us see how do we compute the minimum absolute difference among elements of an array. Given that we are concerened about all the elements of the array tells us that we need to
- Traverse all the elements of the array (so complexity is already O(n))
- Maintain a running counter that captures the minimum difference as we traverse the array - the idea is also to initialize this variable to MAX value so that we can record the minimum difference easily
Note how we try not to overflow the array index (while evaluating arr[i+1]
) by computing modulo n
- the size of the array.