Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 032fdab

Browse files
committedFeb 25, 2025
Add solution #1524
1 parent d544577 commit 032fdab

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@
557557
1507|[Reformat Date](./1507-reformat-date.js)|Easy|
558558
1512|[Number of Good Pairs](./1512-number-of-good-pairs.js)|Easy|
559559
1519|[Number of Nodes in the Sub-Tree With the Same Label](./1519-number-of-nodes-in-the-sub-tree-with-the-same-label.js)|Medium|
560+
1524|[Number of Sub-arrays With Odd Sum](./1524-number-of-sub-arrays-with-odd-sum.js)|Medium|
560561
1528|[Shuffle String](./1528-shuffle-string.js)|Easy|
561562
1535|[Find the Winner of an Array Game](./1535-find-the-winner-of-an-array-game.js)|Medium|
562563
1550|[Three Consecutive Odds](./1550-three-consecutive-odds.js)|Easy|
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 1524. Number of Sub-arrays With Odd Sum
3+
* https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/
4+
* Difficulty: Medium
5+
*
6+
* Given an array of integers arr, return the number of subarrays with an odd sum.
7+
*
8+
* Since the answer can be very large, return it modulo 109 + 7.
9+
*/
10+
11+
/**
12+
* @param {number[]} arr
13+
* @return {number}
14+
*/
15+
var numOfSubarrays = function(arr) {
16+
let even = 0;
17+
let odd = 0;
18+
19+
for (let i = 0, total = 0; i < arr.length; i++) {
20+
total += arr[i];
21+
odd += +(total % 2 === 1);
22+
even += +(total % 2 === 0);
23+
}
24+
25+
return (odd * even + odd) % (1e9 + 7);
26+
};

0 commit comments

Comments
 (0)
Please sign in to comment.