Dsa
Prefix Sum
Medium
Range Sum Frequency
You are given an array of integers and a list of query ranges. For each range query, calculate how many times the sum of the numbers within that range occurs among all subarrays of the given array. Your solution should efficiently compute results without re-evaluating sums for overlapping ranges.
Input:
- The first line contains two integers n and q (1 ≤ n ≤ 100,000, 1 ≤ q ≤ 100,000), the size of the array and the number of queries, respectively.
- The second line contains n integers a[i] (1 ≤ a[i] ≤ 10000) representing the elements of the array.
- The following q lines each contain two integers l and r (1 ≤ l ≤ r ≤ n), the inclusive bounds of the queries.
Output:
- For each query, output a single integer indicating the frequency of the subarray sum corresponding to the given range.
Example:
Input:
5 2
1 2 3 4 5
1 3
2 4
Output:
1
1
Key concepts
prefix_sumarray
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