← Discussions on Plus One
S swathijoshi
07 Oct 2026, 03:37

Simple approach explained for beginners.

A single pass with a running value is enough here. Keep track of the best answer so far and update it as you scan, which gives O(n) time and O(1) extra space.

3 replies

chegde 5 days, 15 hours ago

You can avoid the extra array and do it in O(1) space.

rohit_74 4 days, 16 hours ago

Nice. Got asked a variant of this in an interview.

vamsi.t 4 days, 13 hours ago

For the TLE: you are rebuilding the list inside the loop, move it outside.

Sign in to reply.