Two Sum
We all know what happens when Merry Brandybuck and Peregrin Took come together - DISASTER !
Now imagine your array of numbers as the Fellowship. Your job in Two Sum is basically to identify which number plays Merry and which plays Pippin — the perfect chaos duo — whose combined value leads to something terrible, like touching a palantír, leaking intel to the enemy, or accidentally waking every Orc in Barad-dûr.
Given an array of integers nums and an integer target, return indices of the
two numbers such that they add up to target.
You may assume that each input would have exactly one solution
and you may not use the same element twice.
You can return the answer in any order.
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1] ( AKA Disaster ).
So the natural question is: What would Gandalf do ?
Brute-Force
Before dragging Pippin to Minas Tirith, he definitely tried the brute-force technique of repeatedly explaining why touching random glowing objects is bad.
Two Sum’s brute force works in the same way:
You run a simple nested loop:
The first loop picks a member of the Fellowship.
The second loop pairs him with everyone who comes after.
You add each pair and check if their combined value equals our target, a.k.a. ‘DISASTER.’
When it does, you’ve found your Merry–Pippin duo — so you just return their indices and move on before someone touches a palantír again.
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if nums[i] + nums[j] == target:
return [i,j]
Optimal Solution
The HashMap approach is Gandalf’s version of: ‘I’m too old for this brute-force nonsense.
Instead of checking every possible pairing like an unhinged hobbit, Gandalf just keeps a small cheat-sheet:
As each number walks by, he calculates its perfect disaster partner: target - number
He checks if that partner already walked by.
If yes → those are your Merry and Pippin.
If no → he stores the current number in the map and moves on.
This way, Gandalf finds the disaster duo in linear time.
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
seen = {} # Stores Number -> Index
for i, num in enumerate(nums):
complement = target - num
if complement in seen:
return [seen[complement], i]
seen[num] = i
Time & Space Complexity
Brute Force Approach
Time: O(n²)
Just like walking from the Shire to Mordor twice.
Every new member potentially interacts with all the others — very hobbit behavior.
Space: O(1)
No extra storage except a few variables.
No maps, no extra memory — just pure “two hobbits causing chaos” energy.
Optimal Solution
Time: O(n)
Each lookup & insertion into the map is O(1).
This is basically the “Eagles to Mordor” level of efficiency.
Space: O(n)
We store each number we’ve seen in the HashMap.
Worst case: every member gets added before we find the disastrous duo.
A Wizard’s Closing Notes
So in the end, Two Sum is really just Middle-earth team management:
Try every chaotic combo (brute force), then give up and use Gandalf’s cheat-sheet (HashMap).
You found your Merry–Pippin duo, avoided disaster, and solved the problem in linear time.
Now pack your lembas — we’ve got more LeetCode quests ahead.