Skip to content

Commit 304b398

Browse files
author
richie.p
committed
improve -Implemented the two sum algorithm.
1 parent 76c1340 commit 304b398

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

two_sum.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def two_sum_sorted(array: list[int], target: int) -> str:
2+
"""This algorithm give an integer array, and an integer target.
3+
So, return your target by additional of two indext of the array.
4+
5+
:param array: give a sorted integer array
6+
"""
7+
8+
left = 0
9+
right = len(array) - 1
10+
11+
while left < right:
12+
current = array[left] + array[right]
13+
if current == target:
14+
return "{} + {} = {}".format(array[left], array[right], target)
15+
elif current > target:
16+
right -= 1
17+
elif current < target:
18+
left += 1
19+
return "So, you couldn't fund your target in this list..."
20+

0 commit comments

Comments
 (0)