/*
Given an array arr[] of n elements and q queries . For each query given indices l and r calculate and print the no. of even no in the given range
(l and r both inclusive)
Constraints :- 1 <= n <= 10^5
               1 <= arr[i] <= 10^9
               0 <= l <= r < n
*/
import java.util.*;
import java.lang.*;
import java.io.*;

// The main method must be in a class named "Main".
class Main {

    public static int countEvenNumbersInRange(int[] arr, int l, int r) {
        int count = 0;
        for (int i = l; i <= r; i++) {
            if (arr[i] % 2 == 0) {
                count++;
            }
        }
        return count;
    }

    public static int[] solve(int[] arr, int[] left, int[] right) {
        int[] results = new int[left.length];
        for (int q = 0; q < left.length; q++) {
            int l = left[q];
            int r = right[q];
            results[q] = countEvenNumbersInRange(arr, l, r);
        }
        return results;
    }
    
    public static void main(String[] args) {
        int[] arr = {2,3,5,8,10,2,55,73,77,2,6,77,5,3,24};
        int[] left = { 1 , 2 , 4 };
        int[] right = { 5 , 8 ,12};
        int[] res = solve(arr, left, right);
        System.out.println("Result: " + Arrays.toString(res));
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: