Editorial for DMOPC '18 Contest 5 P3 - A Familiar Problem
Submitting an official solution before solving the problem yourself is a bannable offence.
For the first subtask, we can iterate over all subarrays ~\mathcal O(N^2)~ subarrays, and find their sum in ~\mathcal O(N)~ time, keeping track of the longest one where the sum does not exceed ~M~.
Time Complexity: ~\mathcal O(N^3)~
For the second subtask, we can find the sum in ~\mathcal O(1)~ time by using a prefix sum array.
Time Complexity: ~\mathcal O(N^2)~
For the third subtask, we can use a 2-pointer approach. As the right pointer advances, the left pointer should point to the left-most index where the sum is less than ~M~. Since both pointers will only move from left to right, the time complexity is ~\mathcal O(N)~.
Time Complexity: ~\mathcal O(N)~