mirror of
https://github.com/TheAlgorithms/C.git
synced 2026-02-15 21:51:05 +00:00
* 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>
25 lines
629 B
C
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;
|
|
}
|