Master the most fundamental data structure
Section 4 of 4
Standard array loops are O(n2). These patterns transform slow code into high-speed O(n) systems. This is where you separate the beginners from the experts.
Used primarily on **sorted arrays**. Instead of nested loops, we use two variables pointing to different elements and move them toward each other.
Reduces nested loops **O(n2)** down to a single pass **O(n)**.
Finding pairs, reversing arrays, or removing duplicates in-place.
Target Sum: 15
Imagine a frame (window) that moves over your data. Useful for keeping track of a sub-segment of an array without re-calculating everything inside.
1// Window size k for (int i=0; i < k; i++) sum += arr[i]; for (int i=k; i < n; i++) { sum = sum + arr[i] - arr[i-k]; // Slide! maxSum = max(maxSum, sum); }When the window moves, you only add one new element and subtract one old one. You DON'T re-sum the middle section.
You've mastered contiguous memory, fixed vs dynamic sizing, and the heavy-duty algorithms that make arrays powerful. Next up: **Matrices & 2D Data**.