Given a list of numbers, write a Python program to find the sum of all the elements in the list.
Examples:
Input: arr = [2,4,5,10], i = 1, j = 3
Output: 19
Input: arr = [4,10,5,3,3], i = 3, j = 3
Output: 3
Find the sum of a Sublist Applying Brute Force
In this method, we will be initializing an output variable, say ans=0. Then initialize a loop from index i to j and add the list element to the output.
# Giving Input list and start-end # index values l = [ 4 , 10 , 5 , 3 , 3 ]
i = 3
j = 3
# Initializing an output variable # as 0 ans = 0
# Loop start from i to j+1 for i in range (i, j + 1 ):
ans + = l[i]
# Printing result print (ans)
|
Output:
3
Time Complexity: O(n)
Auxiliary Space: O(1)
Find the sum of a Sublist By using the Cumulative Technique
The cumulative sum technique is used to add all the previous element values to the current index value.
# Giving Input list and start-end # index values l = [ 2 , 4 , 5 , 10 ]
i = 1
j = 3
# Loop starts # Adding the previous values # At every location for x in range ( len (l)):
if (x = = 0 ):
continue
else :
l[x] = l[x] + l[x - 1 ]
# Printing result if (i = = 0 ):
print (l[j])
else :
print (l[j] - l[i - 1 ])
|
Output :
19
Find the sum of a Sublist By using sum() function
sum() function is used to calculate the sum of the list. It takes input as the sub-list.
# Giving input list and start-end # index values l = [ 2 , 4 , 5 , 10 ]
i = 1
j = 3
# Prints sum of sublist from index # i to index j print ( sum (l[i:j + 1 ]))
|
Output :
19
Time Complexity: O(n)
Auxiliary Space: O(n), where n is number of elements in list.
Find the sum of a Sublist By using Math Module
Math module has a special function name fsum(). It also takes the input as the sublist. Then we can use fsum() function, to calculate the sum of the sub-list.
# Input list and start-end index values l = [ 4 , 10 , 5 , 3 , 3 ]
i = 3
j = 3
# Applying fsum() function # i to index j sum = math.fsum(l[i:j + 1 ])
print ( sum )
|
Output :
3
Find the sum of a Sublist using Recursion
A new approach to finding the sum of a sublist would be to use recursive programming, where a recursive function is called on itself to continually add the current element of the sublist to a running total until the end of the sublist is reached.
def sublist_sum(l, i, j):
# base case
if (i = = j):
return l[i]
# recursive step
return l[i] + sublist_sum(l, i + 1 , j)
# Driver code l = [ 2 , 4 , 5 , 10 ]
i = 1
j = 3
print (sublist_sum(l, i, j))
#This code is contributed by Edula Vinay Kumar Reddy |
Time complexity of the sublist_sum() function is O(n), where n is the length of the input list,
Auxiliary Space: O(n).
Find the sum of a Sublist using Slice notation
This problem can be solved using Python’s slice notation. This method allows you to extract a portion of the list between the start and end indices, and then use the built-in sum() function to calculate the sum of the sublist.
l = [ 4 , 10 , 5 , 3 , 3 ]
i = 3
j = 3
# Using slice notation to get sublist sublist = l[i:j + 1 ]
# Using sum() function to get sum of sublist ans = sum (sublist)
# Printing result print (ans)
|
3
Time Complexity: O(n)
Auxiliary Space: O(1)