jayati

Number of Zero-Filled Subarrays

Mar 20th, 2023
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     long long zeroFilledSubarray(vector<int>& nums) {
  4.         long long sum=0;
  5.         vector<long long> v;
  6.         int c=0;
  7.         for(int i=0;i<nums.size();i++)
  8.         {
  9.             if(nums[i]==0)
  10.             {
  11.                 c++;
  12.             }
  13.             else
  14.             {
  15.                 if(v.size()==0 && c==0)
  16.                 {
  17.                     continue;
  18.                 }
  19.                 v.push_back(c);
  20.                 c=0;
  21.             }
  22.         }
  23.         if(nums[nums.size()-1]==0)
  24.         {
  25.             v.push_back(c);
  26.         }
  27.         for(int i=0;i<v.size();i++)
  28.         {
  29.             sum+=((v[i])*(v[i]+1))/2;
  30.         }
  31.         return sum;
  32.     }
  33. };
Advertisement
Add Comment
Please, Sign In to add comment