1. Two Sum ๐Ÿ”

Difficulty: Easy - Tags: Array, Hash Table

LeetCode Problem Link


Problem Statement ๐Ÿ“œ

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

You can return the answer in any order.


Examples ๐ŸŒŸ

๐Ÿ”น Example 1:

Input:

nums = [2,7,11,15], target = 9

Output:

[0,1]

Explanation: nums[0] + nums[1] == 9, so we return [0, 1].

๐Ÿ”น Example 2:

Input:

nums = [3,2,4], target = 6

Output:

[1,2]

๐Ÿ”น Example 3:

Input:

nums = [3,3], target = 6

Output:

[0,1]

Constraints โš™๏ธ

  • 2 <= nums.length <= 10^4

  • -10^9 <= nums[i] <= 10^9

  • -10^9 <= target <= 10^9

  • Only one valid answer exists.


Solution ๐Ÿ’ก

We can use a HashMap to store the difference between the target and each element as we iterate through the array.


Java Solution

import java.util.HashMap;
import java.util.Map;

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[]{};
    }
}

Explanation of the Solution

  1. Initialization:

    • Create a HashMap to store the value and its index as key-value pairs.

  2. Traversal:

    • For each element in nums, calculate the complement (target - nums[i]).

    • Check if the complement exists in the HashMap.

    • If it does, return the current index and the stored index for the complement.

    • Otherwise, add the current element and its index to the HashMap.

  3. Result:

    • Return the indices of the two numbers when the complement is found.


Time Complexity โณ

  • O(n):

    • Each lookup and insertion in the HashMap takes constant time.

Space Complexity ๐Ÿ’พ

  • O(n):

    • Space used by the HashMap to store up to n elements.

You can find the full solution here.

Last updated