Skip to content

ORIGINAL CODING QUESTION

Batch of range total queries

A learning tracker stores n signed daily point changes. It then answers q queries, each asking for the total of the days from index l to index r inclusive (zero-based). Return the sum of all q answers; with no queries return 0.

Open practice workspace

Original exercise · easy. Editorial role relevance does not imply a company has asked this question.

Input and output

n, then n values, then q, then q pairs (l, r). Print one integer.

Constraints

1 ≤ n ≤ 200; -1000 ≤ value ≤ 1000; 0 ≤ q ≤ 200; 0 ≤ l ≤ r < n.

Example 1

Input

5
2 -1 3 4 1
2
0 2
1 4

Output

11

The first query totals 2-1+3 = 4 and the second -1+3+4+1 = 7, so the answer is 11.

Example 2

Input

3
5 5 5
1
1 1

Output

5

One query covering a single day totals 5.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

Summing each query range separately can repeat the same additions many times.

Hint 2

Build prefix[i] = total of the first i values, with prefix[0] = 0.

Hint 3

Each query total is prefix[r + 1] - prefix[l]; add these up over all queries.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →