LeetCode solutions

1631. Number of Sub-arrays With Odd Sum

My accepted Python solution to LeetCode problem 1631, Number of Sub-arrays With Odd Sum, running in 74ms.

  • Difficulty: Medium
  • Python
  • Runtime 74ms
  • Memory 22.1MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 74ms, memory 22.1MB, accepted 2025-12-24.

python
class Solution:
    def numOfSubarrays(self, arr: List[int]) -> int:
        MOD = 10**9 + 7
        # Track count of even and odd prefix sums
        even_count = 1  # Empty prefix has sum 0 (even)
        odd_count = 0
        prefix_sum = 0
        result = 0
        
        for num in arr:
            prefix_sum += num
            
            if prefix_sum % 2 == 0:
                # Current prefix is even, need odd prefix before
                result += odd_count
                even_count += 1
            else:
                # Current prefix is odd, need even prefix before
                result += even_count
                odd_count += 1
            
            result %= MOD
        
        return result

Source