Given an array of integers nums and an integer target, return 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.
You can return the answer in any order.
Example:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
This is the classic Two Sum problem. The key idea is to use a hash map to remember numbers seen so far and their indices, so we can check the complement of each current value in O(1) average time. As we scan the array, if target – nums[i] already exists in the map, we immediately return the two indices; otherwise we store the current number and continue. This reduces the brute-force O(n^2) approach to O(n), which is the standard optimal solution for interviews and online assessments.