aboutsummaryrefslogtreecommitdiff
path: root/number_subarr_odd_sum.py
diff options
context:
space:
mode:
Diffstat (limited to 'number_subarr_odd_sum.py')
-rw-r--r--number_subarr_odd_sum.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/number_subarr_odd_sum.py b/number_subarr_odd_sum.py
new file mode 100644
index 0000000..cf00796
--- /dev/null
+++ b/number_subarr_odd_sum.py
@@ -0,0 +1,28 @@
+# 1524. Number of Sub-arrays With Odd Sum
+
+"""
+given an array of integers 'arr', return the number of subarrays with an odd
+sum. since the answer can be very large, return it modulo 10e9+7.
+"""
+
+
+class Solution(object):
+ def numOfSubarrays(self, arr):
+ """
+ :type arr: List[int]
+ :rtype: int
+ """
+ cnt = [1, 0]
+ curr, ans = 0, 0
+ for i in arr:
+ curr ^= i & 1
+ ans += cnt[1 - curr]
+ cnt[curr] += 1
+ return ans % (10**9 + 7)
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.numOfSubarrays(arr=[1, 3, 5]))
+ print(obj.numOfSubarrays(arr=[2, 4, 6]))
+ print(obj.numOfSubarrays(arr=[1, 2, 3, 4, 5, 6, 7]))