Skip to content

Commit

Permalink
Create 1524.Number-of-Sub-arrays-With-Odd-Sum.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Jul 2, 2021
1 parent a9c71b7 commit 9244709
Showing 1 changed file with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
int numOfSubarrays(vector<int>& arr)
{
int presum = 0;
long odd = 0, even = 1;
long ret = 0;
long M = 1e9+7;
for (int x : arr)
{
presum+=x;
if (presum%2==0)
ret = (ret+odd)%M;
else
ret = (ret+even)%M;
if (presum%2==0)
even+=1;
else
odd+=1;
}
return ret;

}
};

0 comments on commit 9244709

Please sign in to comment.