Given an array of integers nums and an integer target, return the indices of the two numbers that add up to target. Each input has exactly one valid answer, and you may not use the same element twice.
nums = [2,7,11,15], target = 9[0,1]nums = [3,2,4], target = 6[1,2]Try it now — no sign-up needed. Write your solution in Python or JavaScript, run it against test cases, and submit for a verdict, right in your browser.
Open the editor →Arrays give O(1) index access; hash maps and sets give O(1) average lookup, insert, and delete by trading space for speed. Together they're the toolkit for turning brute-force O(n²) comparisons into a single O(n) pass.
Read the full Arrays & Hashing guide →
Original problem on LeetCode ↗