📘
Two Sum
EasyArrays & Hashing
Given an array of integers and a target, return the indices of the two numbers that add up to the target.
Assume exactly one solution exists. Output two indices space-separated.
Example:
Input:
2,7,11,15 9
Output:
0 1
Reasoning:
- The input array is [2,7,11,15] and the target sum is 9.
- We iterate through the array to find two numbers that add up to the target: 2+7=9$, which matches the target.
- The indices of these two numbers in the array are 0 and 1, since array indices start at 0.
- The final output is the space-separated indices: 01
Constraints:
- 2 <= len(nums) <= 10^4
- -10^9 <= nums[i] <= 10^9
- Exactly one valid answer exists
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.