Good Subarrays

Given an array of integers A and a number B

A subarray of an array is said to be good if it fulfills any one of the criteria:

  1. Length of the subarray must be even and the sum of all the elements of the subarray must be less than B.

  2. Length of the subarray must be odd and the sum of all the elements of the subarray must be greater than B.

Your task is to find the count of good subarrays in A. If the count of good subarrays exceeds 10^9 then return 10^9.

Eg:

Input 1:
A = [1, 2, 3, 4, 5]
B = 4
Output 1:
6
Explanation 1:
Even length good subarrays = {1, 2}
Odd length good subarrays = {1, 2, 3}, {1, 2, 3, 4, 5}, {2, 3, 4}, {3, 4, 5}, {5}

Code goes like this:
int main() {
// Write C++ code here
int n = 5;
int a[n] = {5, 10, 15, 20, 25};
int b = 12;
int res = 0;
for(int i=0; i<n; i++){
int currSum = 0;
for(int j=i; j<n; j++){
currSum += a[j];
if((currSum < b && (j-i+1) % 2 == 0) || (currSum > b && (j-i+1) % 2 == 1)){
res++;
}
}
}

cout<<"\nNo of good subarray: "<<res;
return 0;

}