Problem Statement
In the Two Sum problem (find two indices whose values sum to target), what’s the optimal time solution using extra space?
Explanation
Using a hash map to store value→index while iterating allows one‐pass O(n). For each element check if target−value exists in map, then insert current value into map.
Code Solution
SolutionRead Only
Map<Integer,Integer> m = new HashMap<>(); for(int i=0;i<nums.length;i++){ int need=target-nums[i]; if(m.containsKey(need)) return new int[]{m.get(need), i}; m.put(nums[i], i); }