Math for coding interviews/Index arithmetic - pointers as numbers
Array Index Arithmetic: Pointers as Numbers
An array index is just a number pointing to a position. "left + right" divided by 2 gives the middle position. Moving a pointer means adding or subtracting 1 from that number. Two-pointer technique is literally just two variables holding index numbers.
See it for yourself
A row of numbered boxes (0, 1, 2, 3...). Two highlighted arrows sit under two of the boxes. They move toward each other as you click through the algorithm.
Index arithmetic - pointers as numbers
An array index is just a number pointing to a position. "left + right" divided by 2 gives the middle position. Moving a pointer means adding or subtracting 1 from that number. Two-pointer technique is literally just two variables holding index numbers.
Think of it this way: Two people walking toward each other on a straight road. Each person is an index. They meet somewhere in the middle.
For algorithms: The two-pointer pattern avoids the O(n²) brute force by never going backward. Understanding indices as numbers makes this intuitive.
Real-world analogy
Two people walking toward each other on a straight road. Each person is an index. They meet somewhere in the middle.
Why it matters in interviews
The two-pointer pattern avoids the O(n²) brute force by never going backward. Understanding indices as numbers makes this intuitive.