Skip to content

Commit f5e490c

Browse files
authored
Update 1524-number-of-sub-arrays-with-odd-sum.js
1 parent 039362f commit f5e490c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

1524-number-of-sub-arrays-with-odd-sum.js

+26
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1+
/**
2+
* @param {number[]} arr
3+
* @return {number}
4+
*/
5+
const numOfSubarrays = function(arr) {
6+
const n = arr.length, mod = 1e9 + 7
7+
let sum = 0, res = 0, oddCnt = 0, evenCnt = 0
8+
9+
for(let i = 0; i < n; i++) {
10+
const cur = arr[i]
11+
sum += cur
12+
if(sum % 2 === 1) {
13+
res++
14+
res += evenCnt
15+
oddCnt++
16+
} else {
17+
res += oddCnt
18+
evenCnt++
19+
}
20+
}
21+
22+
return res % mod
23+
};
24+
25+
// another
26+
127
/**
228
* @param {number[]} arr
329
* @return {number}

0 commit comments

Comments
 (0)