Arrays and linear scans
Accumulators, indices, and loop invariants
A monitoring job receives request counts in minute order. The report needs the total traffic and the first minute with the highest count. Sorting the readings would do extra work and make the original positions harder to track; storing the whole stream isn't necessary either.
If the busiest value appears again later, the report should still point to its first occurrence. That small requirement decides which comparison belongs in the loop.
Keeping the first busiest minute
Keep the running total and a candidate peak position. Replace the position only for a strictly larger reading; an equal later peak keeps the earlier position. The first reading establishes a candidate, while an empty input never does. This distinguishes an empty report from a real zero-traffic minute.
Summarising the stream once
For readings [6, 2, 6], the total moves through 6, 8, and 14. The busiest position stays at 0: the last reading ties rather than improves the peak. After every step, the retained information describes exactly the prefix already consumed. An empty stream has a total of zero and no busiest position; zero traffic is a real reading, not a missing value.
An accumulator summarizes a prefix. After visiting index i, total covers exactly indices 0..i and the maximum index points to the first maximum in that prefix. Updating only on a strict increase preserves ties. A sort destroys original positions; two separate traversals are correct but unnecessary. Final target: O(n) time and O(1) auxiliary space.
Choose the numeric representation before writing the scan. Widening only after an overflowing int expression does not repair that expression:
import java.util.*;
class Example {
public static void main(String[] args) {
int value = 2_000_000_000;
long widenedFirst = (long) value + value;
long widenedAfter = value + value;
assert widenedFirst == 4_000_000_000L;
assert widenedAfter != widenedFirst;
}
}The addition on widenedFirst is evaluated as long. The other addition wraps as int before assignment. In the exercise, the input is already long[], but accumulators and comparisons must preserve that width. The maximum-index invariant is separate from the total and must preserve ties.
When another pass costs more
Single-pass summaries appear in telemetry reduction, batch statistics, and validation pipelines. Their main benefit is bounded state and a clearly stated prefix invariant; they do not require materializing or reordering the input.
Return Scan(total, firstMaximumIndex). Loads are nonnegative. Empty input gives Scan(0, null); [8,2,8] gives Scan(18,0). Ties choose the first original index.
Numeric arithmetic uses long; indices use int. Inputs follow the declared types and contain no null elements unless the API permits them. Preserve caller-owned collections, including on failure, and use IllegalArgumentException for the documented invalid arguments.
The monitoring job can emit its total and peak position as soon as the final reading arrives.
Practice Arrays and linear scans
Implement the exercise in a full-screen workspace, then run its tests.