diff --git a/medium/number_of_sub_arrays_with_odd_sum.py b/medium/number_of_sub_arrays_with_odd_sum.py new file mode 100644 index 0000000..120b920 --- /dev/null +++ b/medium/number_of_sub_arrays_with_odd_sum.py @@ -0,0 +1,22 @@ +# https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum + +from typing import List + +MOD = 10 ** 9 + 7 +class Solution: + def numOfSubarrays(self, arr: List[int]) -> int: + odd_count = int(arr[0] % 2 == 1) + for i in range(1, len(arr)): + arr[i] += arr[i -1] + odd_count += int(arr[i] % 2 == 1) + + result = odd_count + curr = 0 + for i in range(len(arr)): + if (arr[i] - curr) % 2 == 1: + odd_count -= 1 + odd_count = len(arr) - i - 1 - odd_count + result += odd_count + curr = arr[i] + return result % MOD +