-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathswap_sum.py
More file actions
29 lines (21 loc) · 705 Bytes
/
swap_sum.py
File metadata and controls
29 lines (21 loc) · 705 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
from typing import Tuple, List, Optional
def swap_sum(arr_a: List[int], arr_b: List[int]) -> Optional[Tuple]:
sum_a = sum(arr_a)
sum_b = sum(arr_b)
# sum_a - a + b = sum_b - b + a
# a - b = (sum_a - sum_b) / 2
diff = sum_a - sum_b
if diff % 2 != 0:
return None
target = int(diff / 2)
for i in arr_a:
for j in arr_b:
if i - j == target:
return i, j
return None
def __check_swap(swap: Tuple, arr_a: List[int], arr_b: List[int]):
assert sum(arr_a) - swap[0] + swap[1] == sum(arr_b) - swap[1] + swap[0]
if __name__ == '__main__':
a = [4, 1, 2, 1, 1, 2]
b = [3, 6, 3, 3]
__check_swap(swap_sum(a, b), a, b)