Arrays & Hashing/Easy

Two SumFREE TRACK

Time: O(N)Space: O(N)

Initialize Hash Map

Target is 9. We iterate through nums array and maintain a hash map of { value: index }.

Array Elements & Pointers
2
i=0
7
i=1
11
i=2
15
i=3
Live Variables & Invariants
target:9
i:0
current:-
complement:-
Step 1 / 520%
Solution Code
1
function twoSum(nums: number[], target: number): number[] {
2
  const map = new Map<number, number>();
3
  for (let i = 0; i < nums.length; i++) {
4
    const complement = target - nums[i];
5
    if (map.has(complement)) return [map.get(complement)!, i];
6
    map.set(nums[i], i);
7
  }
8
  return [];
9
}

Custom Test Case Runner

Input your custom values and visualize step-by-step trace

Quick Presets:

AI DSA Coach

Contextual Tutor for Two Sum

Hello! I am your AI DSA Tutor for **Two Sum** (Arrays & Hashing). Ask me anything about this algorithm, time complexity, or request a step hint!