Arrays
TimeO(n)SpaceO(1)
Walk the array once, holding the biggest value seen so far. Each new value is compared against the current champion and takes its place only when it wins — one pass, one remembered number, and nothing behind is ever read twice.
Key idea. Keep one champion and replace it when beaten. The champion already beat everything behind it, so one comparison settles each new value against the whole prefix — n−1 comparisons, which is provably the minimum.
1
Loading…
—