-
Notifications
You must be signed in to change notification settings - Fork 437
/
Copy pathP05_CheckForPairSum.py
42 lines (34 loc) · 1.39 KB
/
P05_CheckForPairSum.py
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
# Author: OMKAR PATHAK
# Given an array A[] of n numbers and another number x, determines whether or not there exist two elements
# in S whose sum is exactly x.
def checkSum(array, sum):
# sort the array in ascending order
# new changes : made use of Python's inbuilt Merge Sort method
# Reason for such change : Worst case Time complexity of Quick Sort is O(n^2) whereas Worst Case Complexity of Merge Sort is O(nlog(n))
array = sorted(array)
leftIndex = 0
rightIndex = len(array) - 1
while leftIndex < rightIndex:
if (array[leftIndex] + array[rightIndex] == sum):
return array[leftIndex], array[rightIndex]
elif(array[leftIndex] + array[rightIndex] < sum):
leftIndex += 1
else:
rightIndex += 1
return False, False
##def quickSort(array):
## if len(array) <= 1:
## return array
## pivot = array[len(array) // 2]
## left = [x for x in array if x < pivot]
## middle = [x for x in array if x == pivot]
## right = [x for x in array if x > pivot]
## return quickSort(left) + middle + quickSort(right)
if __name__ == '__main__':
myArray = [10, 20, 30, 40, 50]
sum = 80
number1, number2 = checkSum(myArray, sum)
if(number1 and number2):
print('Array has elements:', number1, 'and', number2, 'with sum:', sum)
else:
print('Array doesn\'t have elements with the sum:', sum)