← Home

1524. Number of Sub-arrays With Odd Sum

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
DFS + memoizationC++Markdown
152

This is one of those problems where the clean idea matters more than the amount of code. For 1524. Number of Sub-arrays With Odd Sum, the solution in this repository is mainly a DFS + memoization solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: DFS + memoization, dynamic programming.

The notes already sitting in the source point us in the right direction:

  • TLE
  • 131 / 151 test cases passed.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are numOfSubarrays, zeroCounts, oneCounts, cumsums.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//TLE
02//131 / 151 test cases passed.
03class Solution {
04public:
05    int numOfSubarrays(vector<int>& arr) {
06        int MOD = 1e9+7;
07        int n = arr.size();
08        
09        if(all_of(arr.begin(), arr.end(), [](int& e){return !(e&1);})){
10            return 0;
11        }
12        
13        if(all_of(arr.begin(), arr.end(), [&n](int& e){return e&1;})){
14            int ans = 0;
15            
16            while(n > 0){
17                ans += n;
18                n -= 2;
19            }
20            
21            return ans;
22        }
23        
24        int ans = 0;
25        
26        for(int i = 0; i < n; ++i){
27            int subsum = 0;
28            for(int j = i; j < n; ++j){
29                subsum += arr[j];
30                if(subsum&1){
31                    ++ans;
32                    if(ans > MOD) ans -= MOD;
33                }
34            }
35        }
36        
37        return ans;
38    }
39};
40
41//DP
42//https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/discuss/754702/Some-hints-to-help-you-solve-this-problem-on-your-own
43//Runtime: 392 ms, faster than 67.62% of C++ online submissions for Number of Sub-arrays With Odd Sum.
44//Memory Usage: 122.9 MB, less than 100.00% of C++ online submissions for Number of Sub-arrays With Odd Sum.
45class Solution {
46public:
47    int numOfSubarrays(vector<int>& arr) {
48        int n = arr.size();
49        int MOD = 1e9+7;
50        
51        //padding ahead
52        arr.insert(arr.begin(), 0);
53        //dp[i]: dp value of arr[0...i]
54        vector<int> zeroCounts(n+1);
55        vector<int> oneCounts(n+1);
56        vector<int> cumsums(n+1);
57        
58        for(int i = 1; i <= n; ++i){
59            cumsums[i] = (cumsums[i-1] + arr[i])&1;
60            
61            if(!(arr[i]&1)) zeroCounts[i] = 1;
62            else oneCounts[i] = 1;
63            
64            //arr[1...i] = arr[1...i-1] + arr[i]
65            if(!(arr[i]&1)){
66                zeroCounts[i] = (zeroCounts[i]+zeroCounts[i-1])%MOD;
67                oneCounts[i] = (oneCounts[i]+oneCounts[i-1])%MOD;
68            }else if(arr[i]){
69                zeroCounts[i] = (zeroCounts[i]+oneCounts[i-1])%MOD;
70                oneCounts[i] = (oneCounts[i]+zeroCounts[i-1])%MOD;
71            }
72        }
73        
74//         for(int i = 0; i <= n; ++i){
75//             cout << cumsums[i] << " ";
76//         }
77//         cout << endl;
78        
79//         for(int i = 0; i <= n; ++i){
80//             cout << zeroCounts[i] << " ";
81//         }
82//         cout << endl;
83        
84//         for(int i = 0; i <= n; ++i){
85//             cout << oneCounts[i] << " ";
86//         }
87//         cout << endl;
88        
89        //use custom add function to avoid overflow
90        return accumulate(oneCounts.begin(), oneCounts.end(), 0 ,[&MOD](int& a, int& b){return (a+b)%MOD;});
91    }
92};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.