Skip to content

Commit 23bc02b

Browse files
committed
Two sum sorted array
1 parent 92b7b66 commit 23bc02b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def twosum_sorted(nums,target):
2+
lp = 0
3+
rp = len(nums)-1
4+
while lp<rp:
5+
s = nums[lp]+nums[rp]
6+
if s==target:
7+
return [lp+1,rp+1]
8+
elif s<target:
9+
lp+=1
10+
else:
11+
rp-=1
12+
13+
14+
data = sorted(list(map(int,input('enter the number: ').split(','))))
15+
target = int(input('target: '))
16+
print(twosum_sorted(data,target))
17+
18+
# Output:
19+
# enter the number: 2,7,11,15
20+
# target: 9
21+
# [1, 2]

0 commit comments

Comments
 (0)