Find all the pairs whose sum is equal to a given number

 
#For a given integer array, find all the pairs whose sum is equal to a given number.
def find_pairs(array, target_sum):
pairs = []

Sort the array to enable two-pointer approach

array.sort()

left = 0
right = len(array) - 1

while left < right:
current_sum = array[left] + array[right]

if current_sum == target_sum:
pair = (array[left], array[right])
pairs.append(pair)

# Move the pointers to find other possible pairs
left += 1
right -= 1
elif current_sum < target_sum:
left += 1
else:
right -= 1

return pairs
array = [2, 4, 1, 5, 3, 8, 7]
target_sum = 9
result = find_pairs(array, target_sum)
print(result)  

Output: [(1, 8), (2, 7), (3, 6), (4, 5)]