Code
Loading editor...
Tap Analyze to see visualization
Click Analyze to visualize
See step-by-step execution, variables, and output
Variables
Run code to see variables
Output
Console output will appear here
Click Analyze to visualize
See step-by-step execution, variables, and output
Run code to see variables
Console output will appear here
Iterate through the array, storing each number and its index in a hash map. For each element, compute the complement (target - current) and check if it exists in the map. If found, return both indices immediately.
The hash map provides O(1) lookup for complements. By building the map as we iterate, we ensure we only find pairs where the complement appears at a different index.
Updated Feb 2026