Is there any O(n) solution to this problem

Using 2 pointers, pL and pR, tracing cost sum between pL…pR.

Initially pL=pR=1; if cost above limit, advance pL; otherwise advance pR.

Recording the maximum cost below M during the process. When pR > N, you get the global maximum.

1 Like

i applied the same logic .

But is there any better logic?

It is already O(n). what type of logic do you expect?

can u please explain it using kadane’s algorithm?