Skip to content
Related Articles

Related Articles

Improve Article
Python Program for Range sum queries without updates
  • Difficulty Level : Basic
  • Last Updated : 06 Dec, 2018

Given an array arr of integers of size n. We need to compute sum of elements from index i to index j. The queries consisting of i and j index values will be executed multiple times.

Examples:

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)>
Output:
9
12

Please refer complete article on Range sum queries without updates for more details!

My Personal Notes arrow_drop_up
Recommended Articles
Page :