From 6cc210eba8f128b30a91b3fb69f3309763cbd2c4 Mon Sep 17 00:00:00 2001 From: Harsh Vaidya Date: Thu, 20 Jun 2024 11:41:12 +0530 Subject: [PATCH] added Q-2348 of js --- ...2348-number-of-zero-filled-with-subaary.js | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 javascript/2348-number-of-zero-filled-with-subaary.js diff --git a/javascript/2348-number-of-zero-filled-with-subaary.js b/javascript/2348-number-of-zero-filled-with-subaary.js new file mode 100644 index 000000000..4821c1779 --- /dev/null +++ b/javascript/2348-number-of-zero-filled-with-subaary.js @@ -0,0 +1,30 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var zeroFilledSubarray = function(nums) { + let count = 0; + let totalSubarrays = 0; + + for (let i = 0; i < nums.length; i++) { + if (nums[i] === 0) { + count++; + } else { + if (count > 0) { + totalSubarrays += (count * (count + 1)) / 2; + count = 0; + } + } + } + + if (count > 0) { + totalSubarrays += (count * (count + 1)) / 2; + } + + return totalSubarrays; +}; + +// Test cases +console.log(zeroFilledSubarray([1, 3, 0, 0, 2, 0, 0, 4])); +console.log(zeroFilledSubarray([0, 0, 0, 2, 0, 0])); +console.log(zeroFilledSubarray([2, 10, 2019]));