Challenge
Return indices [i, j] with i < j and nums[i] + nums[j] = target. Several pairs may work: choose the pair with the smallest j, and then the smallest i for that j. Return [] if no pair exists. A single element cannot be used twice.
Input and output contract
0 ≤ n ≤ 60,000; nums values and target are integers with absolute value ≤ 1,000,000. Indices are zero-based.
All tests follow this contract. You do not need to validate malformed input. Return the requested result from two_sum; printed output is not your answer. Standard-library tools are allowed. Implement your solution in Python 3.12.
Target complexity: O(n) expected time; O(n) auxiliary space
Return values are checked by type. Use the exact function name. Print output is not your answer. Tests have time and memory limits.
Public examples
Arguments
[[3, 3, 4, 2], 6]
Expected return
[0, 1]
Arguments
[[1, 2, 3], 10]
Expected return
[]
Arguments
[[1, 1, 3], 4]
Expected return
[0, 2]
Hint
Scan j from left to right. Store the earliest index of every value already seen, and check before inserting nums[j].
The worked solution unlocks after completion, or with Premium access.