Two Sum
Easy Arrays & Hashing
nums is a list of whole numbers, and target is a whole number.
Exactly one pair of different positions in nums holds numbers that add up to target.
Return the two positions of that pair, counting from 0, in either order.
Example 1
Input: nums = [14,-3,8,21,5], target = 35
Output: [0,3]
14 and 21 add up to 35, and they sit at positions 0 and 3. No other two numbers reach 35.
Example 2
Input: nums = [5,12,3,9], target = 15
Output: [1,2]
Example 3
Input: nums = [9,9,2], target = 18
Output: [0,1]
Constraints
2 ≤ nums.length ≤ 10⁵-10⁹ ≤ nums[i] ≤ 10⁹-10⁹ ≤ target ≤ 10⁹- Exactly one pair of different positions holds numbers that add up to
target
Solve it here
Write it in Python, Java or C++, run it on your own cases, and submit against hidden tests.
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
See the AI roundWatch the explanation
More Arrays & Hashing
- Longest Consecutive SequenceAI round · Medium
- Majority ElementAI round · Easy
- First Missing PositiveAI round · Hard
- Spiral MatrixMedium
- Set Matrix ZeroesMedium