-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmin_abs.py
More file actions
33 lines (18 loc) · 749 Bytes
/
min_abs.py
File metadata and controls
33 lines (18 loc) · 749 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Solutions of the challenge proposed on Hackerrank at https://www.hackerrank.com/challenges/minimum-absolute-difference-in-an-array/problem
import math
import random
import re
import sys
# Complete the minimumAbsoluteDifference function below.
def minimumAbsoluteDifference(arr):
n = len(arr)
sortedarr = sorted(arr) # sort the array according to ascending order
diff = [] # array in which differences between consecutive numbers will be stored
for i in range(n-1):
difference = abs(sortedarr[i+1] - sortedarr[i])
diff.append(difference)
return min(diff)
if __name__ == '__main__':
n = int(input())
arr = list(map(int, input().rstrip().split()))
print(minimumAbsoluteDifference(arr))