All lessons

2. Arrays & Hashing

Arrays and two pointers

0 of 5 activities0%

Reading 1

Two indices, one pass

Open

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

Open

On a sorted array, two pointers for pair-sum is typically

Fill in 3

Ends

Open

The usual starting indexes for reverse-with-two-pointers are 0 and

Try it 4

Reverse demo

Open

Watch the array reverse.

main.cpp
Loading editor…
Output will appear here.

Assignment 5

Reverse n ints

Open

Read n (1..20), then n integers. Reverse them in place and print separated by spaces.

main.cpp
Loading editor…