← Discussions on Binary Search
K kkumar OFFICIAL
20 Sep 2026, 03:17

Why does my solution TLE on the last test case?

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.

2 replies

poojakhan 3 weeks, 4 days ago

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

kgupta 3 weeks, 1 day ago

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

Sign in to reply.