Python Program for Range sum queries without updates
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)> |
chevron_right
filter_none
Output:
9 12
Please refer complete article on Range sum queries without updates for more details!
Recommended Posts:
- Python Program to print digit pattern
- Python | Extract key-value of dictionary in variables
- Python program to convert time from 12 hour to 24 hour format
- Python | Animated Banner showing 'GeeksForGeeks'
- Python | Segregate list elements by Suffix
- Python Program for Selection Sort
- Python Program for Bubble Sort
- Python Program for Insertion Sort
- Python Program for Heap Sort
- Python Program for Counting Sort
- Python Program for Radix Sort
- Python Program for n-th Fibonacci number
- Python Program for Counting Sort
- Python Program for ShellSort
- Python Program for Longest Common Subsequence



