classSolution{public:longlongzeroFilledSubarray(vector<int>&nums){intn=nums.size();longlongcount=0;// This will count all the possible subarrays of zeroes// Pick a starting point of the subarrayfor(inti=0;i<n;i++){// Pick the ending point of the subarrayfor(intj=i;j<n;j++){// If current element is equal -> 0// Increment the countif(nums[j]==0){count++;// Found a subarray}else{break;// If, not found, break the loop, move to next starting point}}}returncount;// Done}};
classSolution{public:longlongzeroFilledSubarray(vector<int>&nums){intn=nums.size();intcounter=0;// If we see `0` we increment it, Else we see `non-zero` we set it to `0`longlongtotal=0;// This will store the total count of subarrays// We start from left -> rightfor(inti=0;i<n;i++){// If we see a `zero`if(nums[i]==0){counter++;// Increment the `counter`}else{counter=0;// If we see a `non-zero` we set it to `0` }// At last, add the counter value to out `total`total+=counter;}returntotal;// Done!}};
Warning
LINK
You are about to visit a link which has been flagged with the above content warnings. Do you wish to continue?