#K90852. Average of Even Numbers in a Range
Average of Even Numbers in a Range
Average of Even Numbers in a Range
Given an array of integers and a set of queries, your task is to compute the average of all even numbers within a specified range for each query. If there are no even numbers in the given range, output -1.
For a query with indices \(l\) and \(r\) (1-indexed), the answer is defined as:
$$\text{answer} = \begin{cases}\frac{\sum_{i=l}^{r} a_i \cdot \mathbb{1}_{\{a_i \;\text{is even}\}}}{\text{number of even numbers in } [l,r]} &\text{if at least one even number exists},\\ -1 &\text{otherwise}.\end{cases}$$Print each result on a separate line.
inputFormat
The input is given via standard input (stdin) and has the following format:
- The first line contains a single integer \(N\), the number of elements in the array.
- The second line contains \(N\) space-separated integers representing the array elements.
- The third line contains a single integer \(Q\) representing the number of queries.
- Each of the next \(Q\) lines contains two space-separated integers \(l\) and \(r\), representing the boundaries of a query (1-indexed).
outputFormat
For each query, output the average of the even numbers in the specified range on a new line. If there are no even numbers in that range, output -1.
## sample5
1 2 3 4 5
3
1 5
2 4
3 3
3.0
3.0
-1
</p>