forked from algorhythms/HackerRankAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClosest Numbers.py
More file actions
50 lines (40 loc) · 1.17 KB
/
Closest Numbers.py
File metadata and controls
50 lines (40 loc) · 1.17 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
Problem Statement
Sorting is often useful as the first step in many different tasks. The most common task is to make finding things
easier, but there are other uses also.
Challenge
Given a list of unsorted numbers, can you find the numbers that have the smallest absolute difference between them? If
there are multiple pairs, find them all.
"""
__author__ = 'Danyang'
class Solution(object):
def solve(self, cipher):
"""
main solution function
:param cipher: the cipher
"""
N, A = cipher
A.sort()
diff = 1 << 31
lst = []
for i in xrange(N - 1):
b = A[i + 1]
a = A[i]
if abs(a - b) < diff:
diff = abs(a - b)
lst = [a, b]
elif abs(a - b) == diff:
lst.append(a)
lst.append(b)
return " ".join(map(str, lst))
if __name__ == "__main__":
import sys
f = open("1.in", "r")
# f = sys.stdin
solution = Solution()
N = int(f.readline().strip())
A = map(int, f.readline().strip().split(' '))
cipher = N, A
# solve
s = "%s\n" % (solution.solve(cipher))
print s,