generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.c
39 lines (33 loc) · 797 Bytes
/
solution.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdlib.h>
int numberOfSubarrays(int* nums, int numsSize, int k) {
int* evens = malloc(k * sizeof(int));
int evensIndex = k - 1;
int right = numsSize;
int left = right - 1;
while (left >= 0 && evensIndex >= 0) {
if (nums[left] % 2 != 0) {
evens[evensIndex] = right - left;
--evensIndex;
right = left;
}
--left;
}
if (evensIndex >= 0) {
free(evens);
return 0;
}
evensIndex += k;
int totalCount = 0;
while (left >= 0) {
if (nums[left] % 2 != 0) {
totalCount += (right - left) * evens[evensIndex];
evens[evensIndex] = right - left;
if (--evensIndex < 0) evensIndex += k;
right = left;
}
--left;
}
totalCount += (right - left) * evens[evensIndex];
free(evens);
return totalCount;
}