I tried to submit here the following code:

def findMean(self, arr, queries, n, q): # Complete the function sum=0 count=0 li=[] for i in range(0,q,2): l=queries[i] r=queries[i+1] for j in range(l,r+1): sum+=arr[j] count+=1 mean=math.floor(sum/count) li.append(mean) return li

But I am getting some unexpected error, for instance for `2.72727272`

the output needs to be `3`

whereas for `4.6666`

it needs to be `4`

.

Can anyone help me find what math function I need or some other way I should use to solve such errors?

## Answer

I modified your code but it will get TLE.

import math class Solution: def findMean(self, arr, queries, n, q): # Complete the function # sum=0 # count=0 li=[] for i in range(0,q,2): l=queries[i] r=queries[i+1] s = 0 mean = 0 count = r - l + 1 for j in range(l,r+1): s+=arr[j] mean=(s//count) li.append(mean) return li

We can optimize and make it O(q + n) by calculating the prefix sum

You can undestand the concept for the prefix sum method from this link Mean Range Array