Given an array of natural numbers and another natural T, how to find a continuous subarray with a sum less than or equal to T, but the number of elements in this subarray is maximized?
For example, if the given array:
{3, 1, 2, 1, 1} and T = 5 . Then the maximum continual subframe is {1, 2, 1, 1} , because it will contain 5 elements, and the sum is 5.
Another example: {10,1,1,1,1,3,6,7} with T = 8 . Then the maximum adjacent subarray is ${1,1,1,1,3}$
I can do this with operation O(n^2) . However, I am looking for a linear time solution for this problem. Any ideas?
source share