Two Sum
π
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
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
Hash Map: seen { value -> index }
{ empty }
Size: 0
Map is currently empty { }
Step 1 of 5
Speed
1. Overview
Write your lesson content hereβ¦