Advertisement
gubichas

Два числа с суммой N

Oct 19th, 2022 (edited)
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | None | 0 0
  1. def binarySearch(list, low, high, n):
  2.     i = 0
  3.     while (low <= high):
  4.         i = (high + low) // 2
  5.  
  6.         if (list[i] == n):
  7.             return 1
  8.  
  9.         if (list[i] < n):
  10.             low = i + 1
  11.  
  12.         else:
  13.             high = i - 1
  14.     return 0
  15.  
  16.  
  17. def checkTwoSum(list, arr_size, sum):
  18.     list.sort()
  19.     lenght = arr_size - 1
  20.     for i in range(0,lenght):
  21.  
  22.         searchKey = sum - list[i]
  23.         if (binarySearch(list, i + 1, lenght, searchKey) == 1):
  24.             return 1
  25.     return 0
  26.  
  27.  
  28. def print_result(A: list , n: int):
  29.     if (checkTwoSum(A, len(A), n)):
  30.         print("Yes")
  31.     else:
  32.         print("No")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement