How to decrease the speed of the below program? : I am trying to solve the below question.

You are given an array of n numbers and q queries. For each query you have to print the floor of the expected value(mean) of the subarray from L to R.

First line contains two integers N and Q denoting number of array elements and number of queries.

Next line contains N space seperated integers denoting array elements.

Next Q lines contain two integers L and R(indices of the array).

print a single integer denoting the answer. 

I have replaced print with stdout.write() and input with stdin.readline

````
from sys import stdin, stdout

x,y=map(int,stdin.readline().split())

array=[int(x) for x in stdin.readline().split()]

result=[]

sum=0
for i in range(y):
    l,r=map(int,stdin.readline().split())
    for i in range(l-1,r):
        sum = sum + array[i]
    result.append(sum//(r-l+1))
    sum=0

for i in result:
    stdout.write(str(i)+"\n")
````

The time taken by my program is about 8 secs to solve that challenge whereas the required limit is 1.5secs.