sum of Ranges
Given a space-seperated list of integers as integers as input, write a program to and the elements in the given range and print their sum.
You you will be given M multiple range questions, where you should print sum of members that belong to the corresponding range.
Input
The first of input is space-separated integers.
The second line of input is a positive integer M denoting the nuber of quiries.
The next M lines contain two space-seperated integers.
output
The output should be M lines printing the sum of each indices range.
Explanation
In the example, the line of the integers is 12233346 and the 2 indices range.
0 2
1 4
numbers = input().split(" ")
numberQueries=int(input())
rangeNumbers=[]
for i in range(0, numberQueries):
rangeNumbers.append(input())
for i in range(0, numberQueries):
num=rangeNumbers[i].split(" ")
minValue=int(num[0])
maxValue=int(num[1])
sumResult=0
for i in range(0, len(numbers)):
number=int(numbers[i])
if(number>=minValue and number<=maxValue):
sumResult+=number
print(sumResul
Comments
Leave a comment