Dsa
Prefix Sum
Easy
Budget Tracking
You are managing a budget for a project and need to track expenditures over a period of 'n' months. Each month, the expenditure is recorded as an integer, and you are required to calculate the total expenditure over specific intervals of months.
Write a function that reads the number of months and the respective expenditures, followed by several queries. Each query contains two integers representing the start and end of the month interval. Output the total expenditure for each query.
Input:
The first line contains an integer n (1 ≤ n ≤ 10^5), the number of months.
The second line contains n space-separated integers representing the expenditures for each month.
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 ≤ n).
Output:
For each query, output the total expenditure over the specified interval.
Example:
Input:
4
100 200 300 400
2
1 3
2 4
Output:
600
900
Key concepts
prefix_sumfinancial_analysis
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