Two Gates, One Answer — How Opposites Solve Problems Faster
Two Gates, One Decision — How Opposites Work Together
A tale of conflict, coordination, and symmetry — where two fortress gates teach the power of Two-Pointer Algorithms.
Characters: Arjun, Guru Bodhi, Left Gate Guard, Right Gate Guard · Arc: Beginner · Pattern: Two Pointers on Sorted Data (pair-sum)
📜 Sutra of the Fortress — Two Gates, One City
The Gurukul’s sister city was built like a paradox. On the eastern wall rose Gate A, opening toward the mountains and caravans of scholars. On the western wall stood Gate B, facing the desert and its wandering traders.
Each morning, messengers, merchants, and visitors poured in from both gates. Each evening, the city had to decide a single hard question:
If such a pair existed, they would be invited to the inner court for special trade. If not, the opportunity was lost for the day.
Arjun looked at the long list of cargo values. “I know how to check every possible pair,” he groaned. “But that means comparing everyone with everyone. I’ll be here until next sunrise.”
Guru Bodhi smiled in that annoyingly calm way. “Then let the city’s design do half your work,” he said. “When a problem has two sides, you don’t always have to stand in the middle.”
📜 Sutra of Anxiety — “I Can’t Compare Everyone”
Arjun’s tablet of wax was covered with numbers: each entry was the value of a merchant’s cargo. The King’s target value was marked at the top.
“This is my life,” Arjun muttered. “I have a budget and twenty wishlist items. I have a time window and too many things to do. I have piles of courses, books, and projects — and I keep wondering which two together hit the sweet spot.”
He drew a frustrated circle around the numbers.
“Brute force means checking every pair. That’s
O(n²).
I don’t have a squared-sized life.”
📜 Sutra of Strategy — Two Guards, Two Pointers
Guru Bodhi smoothed the dust and wrote the cargo values in a row, from smallest to largest. “The list is already sorted,” he said. “That is the city’s hidden blessing.”
He placed a pebble at the left end and another at the right.
i, the right pebble be
j. They are our two pointers. Together they watch pairs
from opposite sides.”
For each pair values[i] + values[j], only three things can happen:
-
If the sum is too small, the left guard must let in a
bigger cargo → move
ione step right. -
If the sum is too big, the right guard must choose a
smaller cargo → move
jone step left. - If the sum is exactly the target — the two gates agree, and the pair is found.
“Instead of checking every pair,” Guru Bodhi explained,
“you now slide the two ends inward — making at most
O(n) moves.”
🧭 Real-World Scroll — Where Two Pointers Show Up
The fortress is just a metaphor. Two-pointer thinking appears any time you have a sorted line and a constraint involving two ends:
- Finding two expenses that exactly fit a remaining budget.
- Matching two time blocks that add up to a fixed focus window.
- Picking two tasks — one easy, one hard — whose combined effort fits your energy for the evening.
- In code, classic problems like pair-sum, 3-sum (with a third pointer), and many sliding-window optimizations start from this exact idea.
Anywhere you can sort options by size, value, or time and then walk from both ends, two-pointers give you a linear-time way to explore pairs.
🧠 The Ledger of Cargo — Algorithm Scroll
Suppose the cargo values are stored in a sorted list
values,
and the King’s target value is
target.
We want to know whether any pair adds up exactly to
target.
Core Idea
Use two indices:
i at the
left (smallest value) and
j at the
right (largest value). Move them inward based on whether the sum is too small or
too large.
def has_pair_with_sum(values, target):
"""
Given a sorted list `values`, return True if there exist
two distinct elements whose sum equals `target`.
"""
i = 0 # left pointer (Gate A)
j = len(values) - 1 # right pointer (Gate B)
while i < j:
current = values[i] + values[j]
if current == target:
return True # the two gates agree
elif current < target:
i += 1 # sum too small → move left pointer right
else:
j -= 1 # sum too large → move right pointer left
return False # pointers crossed, no pair exists
Complexity
-
Time:
O(n)— each pointer moves at mostnsteps. -
Space:
O(1)— we only store a few indices and sums.
Guru: “Two humble watchers at the edges can see more than a crowd in the middle — so long as the road between them is ordered.”
📜 Sutra of Action — When the Gates Agree
That evening, Arjun rewrote the cargo list in sorted order, placed one guard at each end, and walked the two pointers inward.
After only a handful of steps, the sums matched the King’s target. The chosen pair entered the inner court, and the remaining merchants went on their way.
Later that week, he used the same method on his own life: pairing tasks with time slots, expenses with budget, and learning topics with available energy. Two ends, one decision.