SECTION 18 / 24

Find the maximum and remember every tied student

1 min read
Physics maximum 92 belongs to IDs [5,7,11,18]. A new larger maximum resets the candidate list; an equal maximum extends it.
Physics maximum 92 belongs to IDs [5,7,11,18]. A new larger maximum resets the candidate list; an equal maximum extends it.
REAL COURSE DATA · BUILD AND UNPACK LISTS

Keep every tied maximum ID

Follow one occurrence at a time. Choose another input to test empty lists, repeated IDs and different parameters.

Ready

CURRENT RECORD / VALUE

Ready to begin

No records have been processed. Start with Next step.

What we remember

Processed
0

Predict the next change, then step through the list. The original course records remain unchanged.

Max = empty
TopperIDs = []
foreach Pair in Pairs:
    ID = first(Pair)
    Mark = last(Pair)
    if Max is empty OR Mark > Max:
        Max = Mark
        TopperIDs = [ID]
    else if Mark == Max:
        TopperIDs = TopperIDs ++ [ID]
return (Max, TopperIDs)

When a larger mark arrives, earlier candidates are no longer maximum scorers: replace their list with [ID]. When an equal mark arrives, keep earlier tied IDs and append this one. When a smaller mark arrives, change neither variable. The updates must keep the maximum value and its owner list consistent.

The real Physics result is maximum 92 with IDs [5,7,11,18]: Akshaya, Aditya, Rahul and Abirami. Maths maximum 97 has [11,23]. Chemistry maximum 97 has [18]. These are maximum owners, not top-three-level lists.

An empty-input contract can return an unavailable maximum and an empty owner list. Do not report 0 as a witnessed maximum of an empty list. For known non-negative, non-empty marks a zero sentinel can work for the value, but a clear empty state handles the owner list more reliably.

Try it yourself

Why not simply append every student who ever set a running record?

Show explained answer

Some running records are later beaten. Reset the ID list when a strictly larger mark appears.

Try it yourself

What happens when the next Physics mark is also 92?

Show explained answer

Append that student’s ID while keeping Max = 92.

Try it yourself

Can three independent IFs accidentally append the new record holder twice?

Show explained answer

Yes, if setting Max makes a later equality IF true in the same iteration. Use IF…ELSE IF for these mutually exclusive update cases.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑