Skip to content

Commit 9dd287f

Browse files
Merge branch 'main' into main
2 parents c84daf4 + b3e19ad commit 9dd287f

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
"""
3+
PROBLEM:
4+
We need to find the sum of numbers from(st to ed) in a list.
5+
6+
Approach:
7+
We create a list which stores the sum of numbers from list till that index
8+
9+
To find the sum from index st to ed':
10+
Take total till ed and subtract total till st - 1.
11+
12+
Time complexity:
13+
Initial processing: O(n)
14+
Finding Sum of the range: O(1)
15+
"""
16+
17+
18+
class Processor:
19+
def __init__(self, nums):
20+
self.prefix = []
21+
total_till_now = 0
22+
23+
for n in nums:
24+
total_till_now += n
25+
self.prefix.append(total_till_now)
26+
27+
28+
def sum_of_range(self, st, ed):
29+
30+
if st == 0:
31+
return self.prefix[ed]
32+
33+
return self.prefix[ed] - self.prefix[st-1] #st-1 cause in sum range st is also included
34+
35+
#Example:
36+
# nums = [1, 2, 3, 4, 5]
37+
# trial = Processor(nums)
38+
# print(trial.sum_of_range(2, 4)) # Sum of [3, 4, 5] = 12

contributers.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
| Rushal Verma | Rushalverma | IIIT allahabad |
1717
| Avaneesh Verma | avaneeshk2307-wq | IIIT Allahabad |
1818
| Aditi Guin | GuinAditi | KIIT |
19-
19+
| Saurav Gitte | SauravGitte | IIIT Allahabad |
20+
| Sourish Awasthi | LooninS | IIIT Allahabad |
21+
| Saumya Sood | Marcella2706 | IIIT Allahabad |
2022
<!-- ADD ABOVE THIS -->
2123
<!-- example | Korvac | Betty41 | Reyansh College | -->

0 commit comments

Comments
 (0)