1524. Number of Sub-arrays With Odd Sum Medium
1/**
2 * [1524] Number of Sub-arrays With Odd Sum
3 *
4 * Given an array of integers arr, return the number of subarrays with an odd sum.
5 * Since the answer can be very large, return it modulo 10^9 + 7.
6 *
7 * Example 1:
8 *
9 * Input: arr = [1,3,5]
10 * Output: 4
11 * Explanation: All subarrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]]
12 * All sub-arrays sum are [1,4,9,3,8,5].
13 * Odd sums are [1,9,3,5] so the answer is 4.
14 *
15 * Example 2:
16 *
17 * Input: arr = [2,4,6]
18 * Output: 0
19 * Explanation: All subarrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]]
20 * All sub-arrays sum are [2,6,12,4,10,6].
21 * All sub-arrays have even sum and the answer is 0.
22 *
23 * Example 3:
24 *
25 * Input: arr = [1,2,3,4,5,6,7]
26 * Output: 16
27 *
28 *
29 * Constraints:
30 *
31 * 1 <= arr.length <= 10^5
32 * 1 <= arr[i] <= 100
33 *
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/
38// discuss: https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43 pub fn num_of_subarrays(arr: Vec<i32>) -> i32 {
44 0
45 }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn test_1524() {
56 }
57}
58
Back
© 2025 bowen.ge All Rights Reserved.