
A subarray is called "good" if its length is even and sum is even. Count all good subarrays in array A.
Input: A = 1, 2, 3, 4
Output: 2
Explanation: 1,2,3,4 has even length and sum=10 (even), 2,3,4 has odd length (not good)
Check all subarrays with even length, calculate sum, count if even.
function countGoodSubarrays(A) {
let count = 0;
const N = A.length;
for (let start = 0; start < N; start++) {
let sum = 0;
for (let end = start; end < N; end++) {
sum += A[end];
const length = end - start + 1;
if (length % 2 === 0 && sum % 2 === 0) {
count++;
}
}
}
return count;
}

I'm Rahul, Sr. Software Engineer (SDE II) and passionate content creator. Sharing my expertise in software development to assist learners.
More about me