All lessonsOpen Open Open Open Open
2. Arrays & Hashing
Arrays and two pointers
0 of 5 activities0%
Reading 1
Two indices, one pass
Two pointers often means left and right moving through an array, or slow and fast for cycles and midpoints.
On a sorted array, left/right can find pairs that sum to a target in O(n) instead of O(n^2).
Always keep bounds clear so you do not walk off the array.
// reverse in place
int l = 0, r = n - 1;
while (l < r) {
swap(a[l], a[r]);
l++; r--;
}Check 2
Sorted pair sum
On a sorted array, two pointers for pair-sum is typically
Fill in 3
Ends
Try it 4
Reverse demo
Watch the array reverse.
main.cpp
Loading editor…
Output will appear here.
Assignment 5
Reverse n ints
Read n (1..20), then n integers. Reverse them in place and print separated by spaces.
main.cpp
Loading editor…