Skip to content

Commit f528ce3

Browse files
XenoBytesXpre-commit-ci[bot]MaximSmolskiy
authored
Added dynamic_programming/range_sum_query.py (TheAlgorithms#12592)
* Create prefix_sum.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix pre-commit and ruff errors * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Rename prefix_sum.py to range_sum_query.py * Refactor description * Fix * Refactor code * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Maxim Smolskiy <[email protected]>
1 parent 114d428 commit f528ce3

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

Diff for: dynamic_programming/range_sum_query.py

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
"""
2+
Author: Sanjay Muthu <https://github.com/XenoBytesX>
3+
4+
This is an implementation of the Dynamic Programming solution to the Range Sum Query.
5+
6+
The problem statement is:
7+
Given an array and q queries,
8+
each query stating you to find the sum of elements from l to r (inclusive)
9+
10+
Example:
11+
arr = [1, 4, 6, 2, 61, 12]
12+
queries = 3
13+
l_1 = 2, r_1 = 5
14+
l_2 = 1, r_2 = 5
15+
l_3 = 3, r_3 = 4
16+
17+
as input will return
18+
19+
[81, 85, 63]
20+
21+
as output
22+
23+
0-indexing:
24+
NOTE: 0-indexing means the indexing of the array starts from 0
25+
Example: a = [1, 2, 3, 4, 5, 6]
26+
Here, the 0th index of a is 1,
27+
the 1st index of a is 2,
28+
and so forth
29+
30+
Time Complexity: O(N + Q)
31+
* O(N) pre-calculation time to calculate the prefix sum array
32+
* and O(1) time per each query = O(1 * Q) = O(Q) time
33+
34+
Space Complexity: O(N)
35+
* O(N) to store the prefix sum
36+
37+
Algorithm:
38+
So, first we calculate the prefix sum (dp) of the array.
39+
The prefix sum of the index i is the sum of all elements indexed
40+
from 0 to i (inclusive).
41+
The prefix sum of the index i is the prefix sum of index (i - 1) + the current element.
42+
So, the state of the dp is dp[i] = dp[i - 1] + a[i].
43+
44+
After we calculate the prefix sum,
45+
for each query [l, r]
46+
the answer is dp[r] - dp[l - 1] (we need to be careful because l might be 0).
47+
For example take this array:
48+
[4, 2, 1, 6, 3]
49+
The prefix sum calculated for this array would be:
50+
[4, 4 + 2, 4 + 2 + 1, 4 + 2 + 1 + 6, 4 + 2 + 1 + 6 + 3]
51+
==> [4, 6, 7, 13, 16]
52+
If the query was l = 3, r = 4,
53+
the answer would be 6 + 3 = 9 but this would require O(r - l + 1) time ≈ O(N) time
54+
55+
If we use prefix sums we can find it in O(1) by using the formula
56+
prefix[r] - prefix[l - 1].
57+
This formula works because prefix[r] is the sum of elements from [0, r]
58+
and prefix[l - 1] is the sum of elements from [0, l - 1],
59+
so if we do prefix[r] - prefix[l - 1] it will be
60+
[0, r] - [0, l - 1] = [0, l - 1] + [l, r] - [0, l - 1] = [l, r]
61+
"""
62+
63+
64+
def prefix_sum(array: list[int], queries: list[tuple[int, int]]) -> list[int]:
65+
"""
66+
>>> prefix_sum([1, 4, 6, 2, 61, 12], [(2, 5), (1, 5), (3, 4)])
67+
[81, 85, 63]
68+
>>> prefix_sum([4, 2, 1, 6, 3], [(3, 4), (1, 3), (0, 2)])
69+
[9, 9, 7]
70+
"""
71+
# The prefix sum array
72+
dp = [0] * len(array)
73+
dp[0] = array[0]
74+
for i in range(1, len(array)):
75+
dp[i] = dp[i - 1] + array[i]
76+
77+
# See Algorithm section (Line 44)
78+
result = []
79+
for query in queries:
80+
left, right = query
81+
res = dp[right]
82+
if left > 0:
83+
res -= dp[left - 1]
84+
result.append(res)
85+
86+
return result
87+
88+
89+
if __name__ == "__main__":
90+
import doctest
91+
92+
doctest.testmod()

0 commit comments

Comments
 (0)