Can we find the length of the longest subarray with sum>k efficiently?

Here, ‘k’ can be any integer.

My O(nlogn) solution is as follows :-

Is there any O(n) algorithm for above?


1)I’ve done my research.
2)No, this problem does not belong to any ongoing contest in the world :slight_smile:


Yes, we can! :slight_smile:

Precalc prefix and suffix negative sums sorted by sum and length. It can be done at O(n), because we add new sum to already sorted array only if this one is smaller than last.

Use two pointers technics to parse all pretenders and choose the longest.

can u please send the link of the question : )

try variant of Kadane algorithm.


Question is:-
But after we reach the last step of the solution we realize that we have to find the length of the longest sub-array with sum>-2