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
After reading a prefix, retain its total and the index of its first maximum. The next element extends the total. Replace the maximum index only for a strictly larger value; replacement on equality would select the last maximum. Before reading anything, the index is null, which represents absence without inventing position zero.
For [6n, 2n, 6n], the total advances through 6n, 8n, 14n, while the retained maximum index remains 0. Every exact total must inspect every element: an unseen value can change the answer. This gives a matching linear lower bound.
Values and totals use BigInt so large integers remain exact. Indices and lengths remain Numbers. JavaScript rejects mixed BigInt/Number addition; initialize an accumulator with 0n, and never coerce a BigInt value through Number merely to compare it.
const exact = 10n ** 30n;
console.log(exact + 1n); // 1000000000000000000000000000001n
console.log(typeof exact, typeof [exact].length); // bigint numberWhen another pass costs more
The final scan takes O(n) arithmetic operations and O(1) auxiliary values. BigInt arithmetic itself grows with operand bit length, so the unit-cost arithmetic bound is a model, not a promise of constant hardware time for arbitrary integers. Use this for one whole-array summary. Use prefix sums for repeated range totals, or a different index when updates and ordered queries dominate.
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.