Array Traversal
Read each value once while carrying only the state needed for the answer.
- Recognition signals
- every element matters · one pass is possible · the answer can be updated while scanning
- Core invariant
- After index i, the stored state correctly describes the processed prefix from 0 through i.
- Typical cost
- Usually O(n) time; Usually O(1) auxiliary space
- Common mistakes
- Starting from an unsafe default value · Skipping the first or last index · Forgetting empty-input behavior
for (int index = 0; index < values.length; index++) {
int value = values[index];
// Update the answer using value.
}