Dsa
Prefix Sum
Easy
Rainfall Distribution
You are tasked with analyzing rainfall data recorded over a number of days. Each day's rainfall is recorded as an integer, and you are to determine the total rainfall over specific intervals of days.
Write a function that reads an integer array where each element represents the daily rainfall over 'n' days and an array of queries, where each query consists of two integers indicating the start and end of the interval. Your program should output the total rainfall for each query.
Input:
The first line contains an integer n (1 ≤ n ≤ 10^5), the number of days.
The second line contains n space-separated integers representing the daily rainfall.
The third line contains an integer q (1 ≤ q ≤ 10^5), the number of queries.
The following q lines each contain two space-separated integers, start and end (1 ≤ start ≤ end ≤ n).
Output:
For each query, output the total rainfall over the specified interval.
Example:
Input:
5
1 2 3 4 5
3
1 3
2 5
1 5
Output:
6
14
15
Key concepts
prefix_sumarray_manipulation
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now