Consider an array of integers, . We define the absolute difference between two elements, and (where ), to be the absolute value of .
Given an array of integers, find and print the minimum absolute difference between any two elements in the array. For example, given the array we can create pairs of numbers: and . The absolute differences for these pairs are , and . The minimum absolute difference is .
Function Description
Complete the minimumAbsoluteDifference function in the editor below. It should return an integer that represents the minimum absolute difference between any pair of elements.
minimumAbsoluteDifference has the following parameter(s):
- n: an integer that represents the length of arr
- arr: an array of integers
Input Format
The first line contains a single integer , the size of .
The second line contains space-separated integers .
The second line contains space-separated integers .
Constraints
Output Format
Print the minimum absolute difference between any two elements in the array.
Sample Input 0
3
3 -7 0
Sample Output 0
3
Explanation 0
With integers in our array, we have three possible pairs: , , and . The absolute values of the differences between these pairs are as follows:
Notice that if we were to switch the order of the numbers in these pairs, the resulting absolute values would still be the same. The smallest of these possible absolute differences is .
Sample Input 1
10
-59 -36 -13 1 -53 -92 -2 -96 -54 75
Sample Output 1
1
Explanation 1
The smallest absolute difference is .
Sample Input 2
5
1 -3 71 68 17
Sample Output 2
3
Explanation 2
The minimum absolute difference is .
Minimum Absolute Difference in an Array - Hacker Rank Solution
Observation
The most obvious approach is find the minimum absolute difference between each number in the array and each other number in the array, and then take the minimum of those results. That approach is not ideal because we would need to find the difference between the first element and the elements to its right, the second element and the elements to its right, the third element and the elements to its right, and so on.
Instead, we can simply sort the array to ensure that the absolute difference between each element and its adjacent element(s) is minimal. Working with sorted data allows us to minimize the number of calculations by simply finding the difference between each pair of adjacent elements in the sorted array. For example, if , it becomes once sorted; we then simply find the minimum difference between , , and .
Solution
- Sort the array of numbers using a built-in method in your submission language of choice (you can write a sorting algorithm yourself, but built-in methods are almost always faster).
- Create a variable to track the running minimum absolute difference between any two elements and initialize it to some valid possible minimum (e.g., the absolute difference between the highest and lowest possible values for in the Constraints, the absolute difference between the first and last elements of the sorted array, etc.).
- Iterate through the array and calculate the absolute value of the difference between each pair of adjacent elements. You can alleviate the need to take the absolute value of the difference between and by calculating the difference as ; this is because sorting ensures that is always , so the result of this calculation will always be positive.Check the absolute difference between each adjacent pair of elements against the running minimum absolute difference variable. If the absolute difference between some pair of adjacent elements is less than the value stored in the running minimum variable, set that pair's absolute difference as the new running minimum.
- Print the final value of the running minimum absolute difference variable.
Problem Setter's code:
n = input()
mini = 10**10
arr = map(int, raw_input().split())
arr.sort()
for i in range(n - 1):
mini = min(mini, abs(arr[i] - arr[i+1]))
print mini
Problem Tester's code:
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[n];
for(int arr_i=0; arr_i < n; arr_i++){
a[arr_i] = in.nextInt();
}
in.close();
Arrays.sort(a);
int minDiff = a[n - 1] - a[0];
for (int i = 0; i < n - 1; i++) {
int tmpDiff = a[i + 1] - a[i];
if (tmpDiff < minDiff) {
minDiff = tmpDiff;
}
}
System.out.println(minDiff);
}
}
can you please provide one in c
ReplyDeleteyes
DeleteIf it's not working what should I do
ReplyDelete