From this Hackerrank problem.
Given an array of N integers, find and print the minimum absolute difference between any two elements in the array.
- If there are less than two values in the array, throw an
IllegalArgumentException
. It's hard to find a difference between0
or1
integers. - Sort the input array.
- Set the initial minimum absolute difference to the absolute value of the difference between the value in index
1
and the value in index0
. - Iterate through the sorted array and see if the absolute difference between two indices are less than the current absolute difference. If so, this absolute difference becomes the new minimum value.
- Return the minimum value.
public class MinimumAbsoluteDifferenceIdentifier {
public static int identifyMinimumAbsoluteDifference(int[] values) {
if (values.length <= 1) {
throw new IllegalArgumentException("Array must have at least two elements");
}
Arrays.sort(values);
int minimumAbsoluteDifference = Math.abs(values[1] - values[0]);
for (int i = 1, j = 2; j < values.length; i = j - 1, j++, i++) {
int absoluteDifference = Math.abs(values[j] - values[i]);
if (absoluteDifference < minimumAbsoluteDifference) {
minimumAbsoluteDifference = absoluteDifference;
}
}
return minimumAbsoluteDifference;
}
}