无需更新的范围和查询的Python程序
给定一个大小为 n 的整数数组 arr。我们需要计算从索引 i 到索引 j 的元素总和。由 i 和 j 索引值组成的查询将被执行多次。
例子:
Input : arr[] = {1, 2, 3, 4, 5}
i = 1, j = 3
i = 2, j = 4
Output : 9
12
Input : arr[] = {1, 2, 3, 4, 5}
i = 0, j = 4
i = 1, j = 2
Output : 15
5
# Python program to find sum between two indexes
# when there is no update.
def find_ans(ar, j, k):
l = len(ar)
for i in range(1, l):
ar[i] = ar[i] + ar[i-1]
print(ar[k] - ar[j-1])
return;
pr = [1, 2, 3, 4, 5]
ar = pr[:]
find_ans(ar, 1, 3)
ar = pr[:]
find_ans(ar, 2, 4)
# Code Contributed by Mohit Gupta_OMG <(0_o)>
输出:
9
12
有关更多详细信息,请参阅有关范围和查询的完整文章,无需更新!