Two Sum

01 Min
πŸ”„
Course In Progress In Progress

This article belongs to the DSA for Interviews course, which is actively in development with new lessons rolling out regularly.

Table of Contents 1 sections

Two Sum

LeetCode Problem 1

Two Sum

Find two numbers in the array that add up to the target sum using a hash map to achieve O(N) time complexity.

Initialize Hash Map

target: 9
arrayLength: 4

Target is 9. We initialize an empty hash map 'seen' to store values and their indices.

$ Map<Integer, Integer> seen = new HashMap<>();
Array: nums
2
[0]
7
[1]
11
[2]
15
[3]
Hash Map: seen { value -> index }
{ empty }
Size: 0
Map is currently empty { }
Step 1 of 5
Speed

1. Overview

Write your lesson content here…