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.
nums: an array of integerstarget: an integer sum to search forReturn the indices of the two elements whose values sum to target. The indices may be returned in any order.
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
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.