#167
Two Sum II - Input Array Is Sorted
medium· Two Pointersruns: 0Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers that add up to a specific target. Return the indices of the two numbers as a 1-indexed array [index1, index2]. There is exactly one solution and you may not use the same element twice.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 402
class Solution:
def twoSum(self, numbers: list[int], target: int) -> list[int]:
left, right = 0, len(numbers) - 1
while left < right:
total = numbers[left] + numbers[right]
if total == target:
return [left + 1, right + 1]
if total < target:
left += 1
else:
right -= 1
return []
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.