Two Sum - Problem
The Two Sum problem is a fundamental coding challenge that appears in countless technical interviews! ๐ฏ
Imagine you're shopping with a specific budget. You have a list of item prices and need to find exactly two items that together cost your target amount. That's essentially what this problem asks you to do with numbers!
The Challenge:
Given an array of integers
Key Rules:
โข Each input has exactly one solution - no more, no less
โข You cannot use the same array element twice
โข Return the indices in any order
Example: If
Imagine you're shopping with a specific budget. You have a list of item prices and need to find exactly two items that together cost your target amount. That's essentially what this problem asks you to do with numbers!
The Challenge:
Given an array of integers
nums and an integer target, find the indices of two numbers that add up to the target value.Key Rules:
โข Each input has exactly one solution - no more, no less
โข You cannot use the same array element twice
โข Return the indices in any order
Example: If
nums = [2,7,11,15] and target = 9, then nums[0] + nums[1] = 2 + 7 = 9, so return [0,1]. Input & Output
example_1.py โ Basic Case
$
Input:
nums = [2,7,11,15], target = 9
โบ
Output:
[0,1]
๐ก Note:
Because nums[0] + nums[1] = 2 + 7 = 9, we return [0, 1]. This is the most straightforward case where the first two elements sum to the target.
example_2.py โ Target at End
$
Input:
nums = [3,2,4], target = 6
โบ
Output:
[1,2]
๐ก Note:
nums[1] + nums[2] = 2 + 4 = 6. Note that we cannot use nums[0] + nums[0] = 3 + 3 = 6 because we cannot use the same element twice.
example_3.py โ Minimum Input
$
Input:
nums = [3,3], target = 6
โบ
Output:
[0,1]
๐ก Note:
The only solution is to use both elements: nums[0] + nums[1] = 3 + 3 = 6. This demonstrates the minimum possible input size.
Visualization
Tap to expand
Understanding the Visualization
1
The Brute Force Way
Talk to each person and ask every other person if their numbers sum to the target. Very thorough but exhausting!
2
The Smart Way - Hash Table
As you meet each person, write their number and location in your notebook. For each new person, quickly check if you need their 'complement' number.
3
The Moment of Truth
When you meet someone whose complement you've already seen, you've found your pair! Return both locations.
4
Mission Complete
You've efficiently found the two indices that sum to your target in just one pass through the party!
Key Takeaway
๐ฏ Key Insight: Instead of checking every possible pair (O(nยฒ)), use a hash table to remember what you've seen and instantly find what you need (O(n))!
Time & Space Complexity
Time Complexity
O(n)
Single pass through the array. Each hash map operation (lookup and insert) takes O(1) average time, so total is O(n)
โ Linear Growth
Space Complexity
O(n)
Hash map stores at most n key-value pairs in worst case (when solution is the last pair checked)
โก Linearithmic Space
Constraints
- 2 โค nums.length โค 104
- -109 โค nums[i] โค 109
- -109 โค target โค 109
- Only one valid answer exists
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code