Data Structure & Algorithms
DSA
Arrays prefix sum
Even Numbers in Range

Even numbers in a range

Problem Description

You are given an array A of length N and Q queries given by the 2D array B of size Q×2.

Each query consists of two integers B[i][0] and B[i][1].

For every query, your task is to find the count of even numbers in the range from A[B[i][0]] to A[B[i][1]].

Problem Constraints

1 <= N <= 10^5
1 <= Q <= 10^5
1 <= A[i] <= 10^9
0 <= B[i][0] <= B[i][1] < N

Input Format

First argument A is an array of integers.
Second argument B is a 2D array of integers.

Output Format

Return an array of integers.

Example Input

Input 1:
A = [1, 2, 3, 4, 5]
B = [   [0, 2] 
        [2, 4]
        [1, 4]   ]

Input 2:
A = [2, 1, 8, 3, 9, 6]
B = [   [0, 3]
        [3, 5]
        [1, 3] 
        [2, 4]   ]

Example Output

Output 1:
[1, 1, 2]

Output 2:
[2, 1, 1, 1]

Example Explanation

Explanation 1:

The subarray for the first query is [1, 2, 3] (index 0 to 2) which contains 1 even number.
The subarray for the second query is [3, 4, 5] (index 2 to 4) which contains 1 even number.
The subarray for the third query is [2, 3, 4, 5] (index 1 to 4) which contains 2 even numbers.

Explanation 2:

The subarray for the first query is [2, 1, 8, 3] (index 0 to 3) which contains 2 even numbers.
The subarray for the second query is [3, 9, 6] (index 3 to 5) which contains 1 even number.
The subarray for the third query is [1, 8, 3] (index 1 to 3) which contains 1 even number.
The subarray for the fourth query is [8, 3, 9] (index 2 to 4) which contains 1 even number.

Output

Java
public class Solution {
    public int[] countEvenNumbers(int[] A, int[][] B) {
        int n = A.length;
        int[] result = new int[B.length];
        for (int i = 0; i < B.length; i++) {
            int start = B[i][0];
            int end = B[i][1];
            int count = 0;
            for (int j = start; j <= end; j++) {
                if (A[j] % 2 == 0) {
                    count++;
                }
            }
            result[i] = count;
        }
        return result;
    }
}
Python
def count_even_numbers(A, B):
    result = []
    for query in B:
        start, end = query
        count = sum(1 for i in A[start:end + 1] if i % 2 == 0)
        result.append(count)
    return result
JavaScript
function countEvenNumbers(A, B) {
    let result = [];
    for (let i = 0; i < B.length; i++) {
        let start = B[i][0];
        let end = B[i][1];
        let count = 0;
        for (let j = start; j <= end; j++) {
            if (A[j] % 2 === 0) {
                count++;
            }
        }
        result.push(count);
    }
    return result;
}