Leetcode Problem 1524. Number of Sub-arrays With Odd Sum

1524. Number of Sub-arrays With Odd Sum

Leetcode Solutions

Prefix Sum with Even and Odd Counters

  1. Initialize variables odd_count and even_count to 0, and result to 0.
  2. Iterate through each element num in the array arr.
  3. Add num to the running sum.
  4. If the running sum is odd, increment odd_count and add even_count + 1 to result.
  5. If the running sum is even, increment even_count and add odd_count to result.
  6. Take result modulo 10**9 + 7 to handle large numbers.
  7. Return result.
UML Thumbnail

Dynamic Programming with Modulo Conversion

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...