
The first line of input contains an integer 'T' representing the number of test cases.
The first line of each test case contains a single integer ‘N’ denoting the size of the array.
The second line of each test case contains ‘N’ space-separated integers denoting the number of hours of training.
For each test case, print the length of the longest possible subarray where each day Ninja would get a prize.
The output of each test case will be printed in a separate line.
1 <= T <= 5
1 <= N <= 5000
1 <= HOURS[ i ] <= 24
Where ‘T’ is the number of test cases, ‘N’ is the number of days, and ‘HOURS[ i ]’ is training hours on the i’th day.
Time limit: 1 second.
You do not need to print anything. It has already been taken care of. Just implement the given function.
The idea here is that we will first convert the array in only +1 if the number of hours is greater than 10 and -1 if not.
Then we will find the maximum length subarray with the positive-sum, which we can achieve by storing prefix sum.
We know that if the prefix sum till i’th day is positive, then the maximum length subarray is ‘i + 1’(in 0-based indexing).
Now, if for any pair (i, j) PREFIXSUM[ i ] - PREFIXSUM[ j ] >= one, then it is a valid pair, and to maximize this, we will find the first occurrence of ‘j’.
Algorithm:
The idea here is to use stack data structure and prefix sum of the array to find a valid pair.
We know (i, j) is a valid pair if PREFIXSUM[ j ] - PREFIXSUM[ i ] >= 1 and j > i. Our goal is to optimise j-i over all pairs of (i, j).
Algorithm: