Pattern match🔗 Concept powering this problem
Ω
Big O & Complexity
0 of 10 sections complete
↗🎯 Pattern recognition check
Before coding: identify why big o & complexity works here.
Why algorithm efficiency matters. Time vs space tradeoffs and how to read Big-O notation.
Problem
You have an array of integers and a target sum. Find the two numbers that add up to the target and return their positions (indices).
Each input has exactly one valid answer. You cannot reuse the same element at the same index twice.
Return the pair of indices in any order.
Examples
Example 1
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
// nums[0] + nums[1] = 2 + 7 = 9
Example 2
Input: nums = [3,2,4], target = 6
Output: [1,2]
Constraints
- 2 ≤ nums.length ≤ 10⁴
- -10⁹ ≤ nums[i] ≤ 10⁹
- Only one valid answer exists
⚡Focus on reducing time complexity from the naive solution.