CJCoding With Joseph
15free / daySign inCreate account

Two Sum With Two Pointers

Write two_sum_sorted(values: list[int], target: int) -> tuple[int, int] for a list that is already sorted: return the indexes (left, right) of two values adding up to target, or (-1, -1) when no pair works, using two pointers instead of two loops. Start left at 0 and right at the last index; while left is below right, compare their total with the target: equal means you are done, too small means move left up (every value that way is bigger), too big means move right down. One pass, O(n), against the O(n squared) brute force. Do NOT write a main block or any top-level test code; the tests construct the calls. Input: two lines, a space-separated sorted list of integers and then the target; the tests print the tuple your function returns.

Expected Output:

(1, 3)

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Data Structures & AlgorithmsBig-O
📥 Auto-Input:
1 2 4 7 11 15\n9\n
This input is automatically fed to your program's stdin
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Ctrl+Space to expand shortcuts (pr, inp, def)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.