You are given an array ‘ARR’ of length ‘N’ consisting of only ‘0’s and ‘1’s. Your task is to determine the maximum length of the consecutive number of 1’s.
ARR = [0, 1, 1, 0, 0, 1, 1, 1], here you can see the maximum length of consecutive 1’s is 3. Hence the answer is 3.
The first input line contains a single integer ‘T’, representing the number of test cases.
The first line of each test case contains a single integer ‘N’, representing the array's length.
The second line of each test case contains N space-separated integers representing the elements of the array.
Output Format:
For each test case, print a single integer representing the maximum length of consecutive 1’s in the array.
Output for each test case will be printed on a separate line.
Note :
You do not need to print anything. It has already been taken care of. Just implement the given function.
2
8
0 1 1 0 0 1 1 1
4
0 1 1 0
3
2
For the first test case, ‘ARR’ = [0, 1, 1, 0, 0, 1, 1, 1], here you can see the maximum length of consecutive 1’s is 3 when we select ARR[5], ARR[6] and ARR[7]. Hence the answer is 3.
For the second test, ‘ARR’ = [0, 1, 1, 0], here you can see the maximum length of consecutive 1’s is 2 when we select ARR[1] and ARR[2]. Hence the answer is 2.
2
6
1 1 1 1 0 0
4
1 1 1 1
4
4
1 ≤ T ≤ 10
1 ≤ N ≤ 1000
ARR[i] = {0, 1}
Time Limit: 1 sec
Try to iterate over every subarray of the array.
Approach:
In this approach, we will iterate over all the possible subarrays of the array starting at each index. If we find an 0 in the subarray we will stop as we have reached the maximum possible consecutive 1’s starting from i'th index, then we will check all the subarrays starting from the next index.
Algorithm:
O( N ^ 2 ), where N is the length of the array
In the worst case, when the array consists of only 1's, we will be generating all the ~(N^2) subarrays.Hence the final time complexity is O(N^2).
O( 1 )
We are not using any extra space.Hence the overall space complexity is O(1).