Given an integer array nums and an integer target, return the indices of the two numbers such that they add up to target. You may assume that each input has exactly one solution, and you may not use the same element twice.
This is the canonical one-pass hash map problem: as you scan nums, for each x check if target - x has already been seen; otherwise store x → index for future lookups.
2 ≤ nums.length ≤ 10⁵-10⁹ ≤ nums[i], target ≤ 10⁹target.[i, j] with i < j.Use a hash map from value → index for O(1) complement lookups.
Pick any number x in nums. Its partner isn't a mystery: it has to be exactly target - x. So the real question for each number is a yes-or-no one: does target - x appear somewhere else in the array, and where?
Time O(n)Space O(n)
The full walkthrough and code in five languages, free to read