๐ WAYPOINT LESSON
Array Mechanics
โญ beginnerโณ 11 min read๐ Lesson 7 of 281
Declaration, indexing from 0, traversal, and the bounds rule that prevents runtime errors.
Declaration and indexing
int n; in >> n;
vector<long long> a(n); // indices 0 .. n-1
for (long long& x : a) in >> x;
Indices run 0 to n-1. Accessing a[n] is undefined behavior โ the
#1 cause of Runtime Error. When a problem says "the i-th element" with
1-based counting, translate: element i lives at a[i-1].
Traversal styles
for (int i = 0; i < n; ++i) ... // when you need the index
for (long long x : a) ... // values only
for (int i = n - 1; i >= 0; --i) // backwards
min/max with a running best
long long best = a[0]; // NOT 0 โ values may be negative!
for (long long x : a) best = max(best, x);
Initializing best = 0 fails the moment all values are negative. Start
from the first element (or -infinity).
Reverse and rotate
reverse(a.begin(), a.end()); // in place
rotate(a.begin(), a.begin() + k, a.end()); // left-rotate by k