fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int Mod=1e9+7;
  5. const ll INF = 10000000000000;
  6. const int N = 1e6+7;
  7.  
  8. void solve() {
  9. int n;
  10. cin >> n;
  11. vector<int> a(n);
  12. int cnt = 0;
  13. for(int i=0;i<n;i++){
  14. cin >> a[i];
  15. cnt+=(a[i]==0)?1:0;
  16. }
  17. if(cnt==n) cout << 0 << '\n';
  18. else if(cnt<1) cout << 1 << '\n';
  19. else if(cnt>=1){
  20. int r=n-1,l=0;
  21. while(a[r]==0 && r>0) r--;
  22. while(a[l]==0 && l<n) l++;
  23. if(l+n-r-1==cnt) cout << 1 << '\n';
  24. else cout << 2 << '\n';
  25. }
  26. else cout << 2 << '\n';
  27. }
  28.  
  29. int main(){
  30. ios::sync_with_stdio(false);
  31. cin.tie(nullptr);
  32.  
  33. int t;
  34. cin >> t;
  35. while (t--) solve();
  36.  
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0.01s 5316KB
stdin
10
4
0 1 2 3
6
0 0 0 0 0 0
5
1 0 1 0 1
5
3 1 4 1 5
4
3 2 1 0
7
9 100 0 89 12 2 3
4
0 3 9 0
7
0 7 0 2 0 7 0
1
0
2
0 1
stdout
1
0
2
1
1
2
1
2
0
1