Dsa
Prefix Sum
Medium
Range Sum Query
Implement a function that handles range sum queries on an integer array. The function should read an initial array and then process several queries that ask for the sum of elements between two indices, inclusive. Your task is to return the result of each query efficiently.
Input Format:
- The first line contains an integer n (the length of the array).
- The second line contains n space-separated integers (the elements of the array).
- The third line contains an integer q (the number of queries).
- The next q lines each contain two integers l and r (the range for the query).
Output Format:
- For each query, output a single integer representing the sum of the specified range.
Example:
Input:
5
1 3 5 7 9
2
1 3
0 4
Output:
15
25
Key concepts
prefix_sumqueryrange_sum
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