Minimum Absolute Difference
EasyPractice library · +10 XPgreedysortingarrays
Given an array of N distinct integers, find the minimum absolute difference between any two elements in the array. The absolute difference between two numbers a and b is |a - b|.
Input
The first line contains an integer N. The second line contains N distinct integers of the array.
Output
Print a single integer representing the minimum absolute difference found.
Constraints
2 <= N <= 1000 -10^9 <= array elements <= 10^9
Time limit 1 s (native code; slower languages get more)Memory 256 MB13 hidden tests
Example 1
Input
5 1 5 3 10 7
Output
2
Sorted array is [1, 3, 5, 7, 10]. Differences are |3-1|=2, |5-3|=2, |7-5|=2, |10-7|=3. Minimum is 2.
Example 2
Input
3 100 200 50
Output
50
Sorted array is [50, 100, 200]. Differences are |100-50|=50, |200-100|=100. Minimum is 50.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
5 1 5 3 10 7
Example 2 input
3 100 200 50