Input: ‘N’ = 2, ‘Q’ = 2, ‘ARR’ = [7, 5], ‘QUERIES’ = [[1, 8], [2, 1, 3]].
Output: 6
In this case, the array ‘ARR’ after the first query will be ‘ARR’ = [7, 5, 8].
In the second query, the average will be = the sum of hits from day 1 to day 3 divided by 3
i.e. (7+5+8)/3 = 20/3 = 6.6666… when rounded gives 6. Hence the output will be 6.
The first line will contain the integer ‘T’, the number of test cases.
The first line of each test case contains two integers, ‘N’ and ‘Q’ separated by spaces.
Followed by a line containing space-separated ‘N’ non-negative integers, denoting the elements of the array ‘ARR’.
Followed by ‘Q’ lines, each of the following types:
(1) If its query ‘1’, it will consist of two space-separated numbers, ‘1’ and ‘X’, where ‘X’ denotes the number to be added at the end of the existing array ‘ARR’.
(2) If its query ‘2’, it will consist of three space-separated numbers, ‘2’ and ‘L’ and ‘R’.
For each test case, print the average count of hits of stumps per day for every query of type ‘2’.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 10
1 <= ‘N’ <= 10^5
1 <= ‘Q’ <= 10^5
0 <= ‘ARR[i]’ <= 10^4
0 <= ‘X’ <= 10^4
1 <= ‘L’ <= ‘R' <= Total number of elements in the array ‘ARR’, before this operation.
It is guaranteed that sum of ‘N’ over all test cases is <= 10^5
It is guaranteed that sum of ‘N’ over all test cases is <= 10^5
It is guaranteed that there will be atleast one query of type ‘2’.
Time Limit: 1 sec
The idea for this approach is to use a list data structure (here list is the dynamic array) to add the elements into an existing array and then iterate on the array for the given range and find out its average.
We can first create a copy of the given array ‘ARR’ into our list and then perform the queries in the given order.
Algorithm:
// The function will return the answers for all queries of type ‘2’.
Int[] getAverage(N, Q, ARR, QUERIES)
The idea for this approach is to use the prefix sum technique, using this we can find the sum for any range in O( 1 ) time. For that, we will need to pre-process the sum from ‘0’ to ‘i’ for ‘0’ <= ‘i’ <= ‘N-1’ and store it in an array. Let’s call it the ‘prefixSum’ array. We will use a dynamic array which we have called a list here.
Whenever there is a query of type 1, we can find the sum up to the last element of array ‘ARR’, from the value stored at the last element of the array ‘prefixSum’. We will push the sum of the new element and the sum of all elements up to now and insert it at the end of the array ‘prefixSum’.
For query of type 2, we can find the sum of range ‘L’ to ‘R’ using the ‘prefixSum’ array. This can be found by adding the sum of all elements up to index ‘R’ and removing the sum of all elements up to index ‘L-1’. In this way, we get the sum of all elements in the range ‘[L, R]’ and then divide it by ‘R - L + 1’ to get the average.
One of the edge cases for this approach is when ‘L = 1’, in this case, we don’t have to remove the sum of any elements before ‘1’.
Algorithm:
// The function will return the answers for all queries of type ‘2’.
Int[] getAverage(N, Q, ARR, QUERIES)