Foundations
Linear Structures
Hashing
Array and String Patterns
Recursion, Search, and Sorting
Problem Solving Patterns
Algorithm Searching
Sorting Algorithms
Array Algorithms
Grid Algorithms
Linear Structure Algorithms
Hashing Algorithms
Recursion and Backtracking
Greedy Algorithms
Tree and Heap Algorithms
Graph Algorithms
Dynamic Programming Algorithms
String Algorithms
Math Algorithms
Keyboard ready
Space plays. Arrow keys step. R restarts.
LAB Two Sum Lab
Hashing
Problem shape
Beginner
4
8
15
16
23
42
Core idea
Store seen values in a hash map and look for the complement of each value.
Step 1 / 5
Problem shape
Hashing Beginner
Two Sum
Welcome to the Two Sum algorithm lab. Store seen values in a hash map and look for the complement of each value. Use it when: Use when you need a pair that adds to a target and values are not necessarily sorted. The time complexity is O(n), and the space complexity is O(n). The pseudocode is: seen = empty map for i from 0 to n - 1 need = target - a[i] if need in seen return [seen[need], i] seen[a[i]] = i As you practice, track the state that changes and the condition that makes the algorithm stop.
Time
O(n)
Space
O(n)
Pseudocode
seen = empty map
for i from 0 to n - 1
need = target - a[i]
if need in seen return [seen[need], i]
seen[a[i]] = i 1.0x