Skip to content

Commit 5a6670a

Browse files
1524_Number_of_SubArrays_with_Odd_Sum.java
1 parent c77f5be commit 5a6670a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Problem Number: 1524
2+
3+
// Problem Number of Sub-Array with Odd Sum.
4+
5+
class Solution {
6+
public int numOfSubarrays(int[] arr) {
7+
int MOD = 1000000007; // since answer can be very long thus moduls 10^9+7 (as directed)
8+
int odd_count = 0;
9+
int even_count = 1;
10+
int prefix_sum = 0;
11+
int result = 0;
12+
13+
for (int i=0; i<arr.length; i++) {
14+
prefix_sum += arr[i];
15+
16+
if (prefix_sum % 2 == 0) {
17+
result = (result + odd_count) % MOD;
18+
even_count++;
19+
} else {
20+
result = (result + even_count) % MOD;
21+
odd_count++;
22+
}
23+
}
24+
25+
return result;
26+
27+
28+
}
29+
}

0 commit comments

Comments
 (0)