leetcode

Two Sum - Link

Question Description

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 would have exactly one solution, and you may not use the same element twice.


Constraints


Approach

Use a HashMap to store visited numbers and their indices. For each number, check if target - current exists in the map. If it does, return the stored index and current index. Otherwise, store the current number with its index.

This approach works because we need to find two numbers that sum to target, and using a hashmap allows O(1) lookups for the complement. Alternative approaches like brute force (O(n²)) would be too slow for large inputs, and sorting would require additional complexity to track original indices.


Dry Run

Example Input: nums = [2,7,11,15], target = 9

Step-by-step execution:

Final Answer = [0,1]


Solution

class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int complement = target - nums[i];
            if (map.containsKey(complement)) {
                return new int[] { map.get(complement), i };
            }
            map.put(nums[i], i);
        }
        return new int[] {};
    }
}

Time and Space Complexity