Question
You are given a binary array a of n elements, a binary array is an array consisting only of 0s and 1s. A blank space is a segment of consecutive elements consisting of only 0s.Your task is to find the length of the longest blank space.
Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases.The first line of each test case contains a single integer n (1≤n≤100) — the length of the array.The second line of each test case contains n space-separated integers ai (0≤ai≤1) — the elements of the array.
Output
For each test case, output a single integer — the length of the longest blank space.
Examples
551 0 0 1 040 1 1 11031 1 191 0 0 0 1 0 0 0 1
2 1 1 0 3
Explanation
Take a input n for the array size n then take input of array n size of 0s and 1s.
first declare cnt = 0 and then traverse the array where it will get arr[i] == 1 you have to push the cnt into array and make cnt = 0 and put continue; i.e
- if(arr[i]==1){
- ans.push_back(cnt);
- cnt=0;
- continue;
- }
in this way.
if zero then contiue; and code becomes
vector<int> ans; int cnt=0;
for(int i=0;i<n;i++){
if(arr[i]==1){
ans.push_back(cnt);
cnt=0;
continue;
}
else{
cnt++;
}
}
simply return maximum elements from vector ans.
Code
#include<bits/stdc++.h>
using namespace std;
#define test long long T;cin>>T;while(T--)
#define all(x) (x).begin(), (x).end()
void solve(){
int n; cin>>n;
int arr[n];
for(int i=0;i<n;i++) cin>>arr[i];
vector<int> ans; int cnt=0;
for(int i=0;i<n;i++){
if(arr[i]==1){
ans.push_back(cnt);
cnt=0;
continue;
}
else{
cnt++;
}
}
ans.push_back(cnt);
int finalAns = *max_element(all(ans));
cout<<finalAns<<endl;
}
signed main() {
test
//(if you want to take the more test cases you may uncomment it out)
solve();
}