


The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then, the T test cases follow.
The first line of each test case or query contains an integer 'N' representing the size of the array (ARR).
The second line contains 'N' single space-separated binary values, representing the elements in the array.
The third line contains the value of 'K'.
For each test case, return the length of the longest subarray whose all elements are 1.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 5 * 10^4
0 <= Arr[i] <= 1
0 <= K <= N
Time Limit: 1 sec
The simplest way to find the required subarray would be to consider all the possible subarrays and compare the length at every point and store the longest length. If at any point, the current element is 0 and the value of ‘K’ is greater than 0, use it to convert the current element to 1. Else if the value of K is 0 , iterate further to find the next possible subarray.
Algorithm :
The idea is to iterate the whole array and push the indices having value as zero of the subarray considered in a queue. Keep on iterating the array until the size of the queue is less than ‘K’. When the size of the queue becomes equal to ‘K’, update the starting index of the subarray under consideration and pop an element from the queue and push the value of current index in the queue. Check at every step for maximum size.
Algorithm :
The idea is to use Two - Pointer approach. Let us take a subarray [l,r] which contains at-most ‘K’ zeroes. Let our left pointer be ‘l’ and right pointer be ‘r’. We always maintain our subsegment [l,r] to contain no more than ‘K’ zeroes by moving the left pointer ‘l’ accordingly. Check at every step for maximum size i.e. ‘r-l+1’.
Algorithm :