forked from mbobesic/algorithms-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinAbsSumOfTwo.py
More file actions
26 lines (19 loc) · 787 Bytes
/
MinAbsSumOfTwo.py
File metadata and controls
26 lines (19 loc) · 787 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
# link: https://codility.com/demo/take-sample-test/min_abs_sum_of_two
# name: Min Abs Sum Of Two
# you can use print for debugging purposes, e.g.
# print "this is a debug message"
def solution(A):
# write your code in Python 2.7
a_sorted = sorted(A)
end = len(A) - 1
result = abs(a_sorted[0] + a_sorted[end])
for index in xrange(len(A)):
current = end
current_result = abs(a_sorted[index] + a_sorted[current])
while current >= 0 and abs(a_sorted[index] + a_sorted[current]) <= current_result:
current_result = abs(a_sorted[index] + a_sorted[current])
end = current
current -= 1
if current_result < result:
result = current_result
return result