Find two distinct numbers in an array whose sum equals a target value.
Given an integer array nums and an integer target, find the indices of the two distinct elements such that their values add up to target.
You may assume that exactly one valid pair exists for the standard problem variant. Return the indices in any order.
Input Format
nums: an array of integerstarget: an integer sum to search for
Output Format
Return the indices of the two elements whose values sum to target. The indices may be returned in any order.
Constraints
- The two indices must be different
- Each input is assumed to have a valid answer for the standard variant
- Aim for better than time if possible
Example 1
Input
nums = [2,7,11,15], target = 9
Output
[0,1]
Explanation
nums[0] + nums[1] = 2 + 7 = 9. Indices can be returned in any order.
Example 2
Input
nums = [3,2,4], target = 6
Output
[1,2]
Explanation
2 + 4 = 6, so the indices 1 and 2 form a valid answer.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.