Files
C/leetcode/src/1524.c
Alexander Pantyukhin ea775e7a04 feat: add Number of Sub-arrays With Odd Sum (#1139)
* add Number of Sub-arrays With Odd Sum

* Update 1524.c

add new line at the end

* Rename README.md to DIRECTORY.md

* chore: apply suggestions from code review

Co-authored-by: David Leal <halfpacho@gmail.com>
2022-11-18 14:09:11 -06:00

25 lines
629 B
C

// Counting whole summ. evens sums number and odd summs number.
// Runtime: O(n),
// Space: O(1)
int numOfSubarrays(int* arr, int arrSize){
int result = 0;
int curSumm = 0;
int currOddSumms = 0;
int currEvenSumm = 0;
int modulo = 1000000000 + 7;
for(int i = 0; i < arrSize; i++){
curSumm += arr[i];
if (curSumm % 2 == 0){
currEvenSumm++;
result = (result + currOddSumms) % modulo;
}
else {
currOddSumms++;
result = (result + 1 + currEvenSumm) % modulo;
}
}
return result % modulo;
}