The value of the sum can be very large, return the answer as modulus 10^9+7.
The first line of input contains a single integer T, representing the number of test cases or queries to be run.
Then the T test cases follow.
The first line of each test case contains a single integer N, denoting the size of the array “A”.
The second line of each test case contains N single space-separated integers, elements of the array “A”.
The third line of each test case contains a single integer Q, denoting the number of queries.
Then each of the Q lines of each test case contains two single space-separated integers L, and R denoting the left and the right index of the infinite array “B” whose sum is to be returned.
For each test case, print Q space-separated integers that denote the answers of the given Q queries.
Print the answer to each test case in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N <= 10^4
1 <= A[i] <= 10^9
1 <= Q <= 10^4
1 <= L <= R <= 10^18
Time Limit: 1sec
The better idea is to first create the sum array in which sumArray[i] stores the sum from (A[0]+....+A[i]). Now instead of iterating from L to R like in the previous approach, we find the sum using our sumArray[].
Let’s take an example, where array A = [1,2,3] and we have to find the sum of the subarray of the infinite array(as shown in below fig) from index 3 to 10 (0-based indexing).
So this way we can find the sum without iterating from L to R.
Longest Subarray With Zero Sum
Randomly Sorted
Merge Two Sorted Arrays Without Extra Space
Ninja And The Strictly Increasing Array
Negative To The End