From f04d6a92d9206a4fe67c50a4f4ad9f6067817f27 Mon Sep 17 00:00:00 2001 From: aadil42 <77232799+aadil42@users.noreply.github.com> Date: Tue, 25 Feb 2025 13:13:29 +0530 Subject: [PATCH] Create 1524-number-of-sub-arrays-with-odd-sum.js Solved number-of-sub-arrays-with-odd-sum --- .../1524-number-of-sub-arrays-with-odd-sum.js | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 javascript/1524-number-of-sub-arrays-with-odd-sum.js diff --git a/javascript/1524-number-of-sub-arrays-with-odd-sum.js b/javascript/1524-number-of-sub-arrays-with-odd-sum.js new file mode 100644 index 000000000..dfbd4171b --- /dev/null +++ b/javascript/1524-number-of-sub-arrays-with-odd-sum.js @@ -0,0 +1,33 @@ +/** + * Sliding Window | Counting + * Time O(n) | Space O(1) + * https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum + * @param {number[]} arr + * @return {number} + */ +var numOfSubarrays = function(arr) { + + let odds = 0; + let evens = 0; + let total = 0; + let currTotal = 0; + const mod = 10**9 + 7; + + for (const num of arr) { + currTotal += num; + + // if odd + if (currTotal%2) { + odds++; + total = (total + 1 + evens) % mod; + } + + // if even + if (!(currTotal%2)) { + evens++; + total = (total + odds) % mod; + } + } + + return total%mod; +};