diff --git a/leetcode/DIRECTORY.md b/leetcode/DIRECTORY.md index b043ca44..091453f8 100644 --- a/leetcode/DIRECTORY.md +++ b/leetcode/DIRECTORY.md @@ -95,5 +95,6 @@ | 1184 | [Distance Between Bus Stops](https://leetcode.com/problems/distance-between-bus-stops/) | [C](./src/1184.c) | Easy | | 1189 | [Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/) | [C](./src/1189.c) | Easy | | 1207 | [Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/) | [C](./src/1207.c) | Easy | +| 1524 | [Number of Sub-arrays With Odd Sum](https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/) | [C](./src/1524.c) | Medium | | 1752 | [Check if Array Is Sorted and Rotated](https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/) | [C](./src/1752.c) | Easy | | 2130 | [Maximum Twin Sum of a Linked List](https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/) | [C](./src/2130.c) | Medium | diff --git a/leetcode/src/1524.c b/leetcode/src/1524.c new file mode 100644 index 00000000..46ad3e46 --- /dev/null +++ b/leetcode/src/1524.c @@ -0,0 +1,24 @@ +// 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; +}