Leetcode 1631 Solution

This article provides solution to leetcode question 1631 (number-of-sub-arrays-with-odd-sum)

https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum

Solution

class Solution:
    def numOfSubarrays(self, arr: List[int]) -> int:
        odd_cnt = 0
        even_cnt = 1

        ans = 0
        s = 0
        for v in arr:
            s += v

            if s % 2 == 0:
                ans = (odd_cnt + ans) % 1000000007
                even_cnt += 1
            else:
                ans = (even_cnt + ans) % 1000000007
                odd_cnt += 1

        return ans