Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python Program to Find Largest Element in an Array
Next article icon

Given an array of integers, find the sum of its elements.

Examples:

Input : arr[] = {1, 2, 3}
Output : 6
Explanation: 1 + 2 + 3 = 6

ThisPython program calculates the sum of an array by iterating through each element and adding it to a running total. The sum is then returned. An example usage is provided to demonstrate the calculation of the sum for a given array.

Python Program to Find Sum of Array

Iterating through the array and adding each element to the sum variable and finally displaying the sum.

Python3
# Python 3 code to find sum# of elements in given arraydef_sum(arr):# initialize a variable# to store the sum# while iterating through# the array latersum=0# iterate through the array# and add each element to the sum variable# one at a timeforiinarr:sum=sum+ireturn(sum)# main functionif__name__=="__main__":# input values to listarr=[12,3,4,15]# calculating length of arrayn=len(arr)# calling function ans store the sum in ansans=_sum(arr)# display sumprint('Sum of the array is ',ans)

Output
Sum of the array is  34

Time complexity: O(n), 
Auxiliary Space: O(1)

Python Program to Find Sum of Array Using sum()

Using the built-in functionsum().Python provides an inbuilt function sum() which sums up the numbers in the list. 

Python3
# Python 3 code to find sum# of elements in given array# input values to listarr=[12,3,4,15]# sum() is an inbuilt function in python that adds# all the elements in list,set and tuples and returns# the valueans=sum(arr)# display sumprint('Sum of the array is ',ans)

Output
Sum of the array is  34

Time complexity: O(n) 
Auxiliary Space: O(1)

Python Program to Find Sum of Array Using reduce method

Using thereducemethod. Array.reduce() method is used to iterate over the array and get the summarized result from all elements of array.

Python
fromfunctoolsimportreduce# Python 3 code to find sum# of elements in given arraydef_sum(arr):# iterate over array# using reduce and get# sum on accumulatorsum=reduce(lambdaa,b:a+b,arr)return(sum)# driver functionarr=[]# input values to listarr=[12,3,4,15]# calculating length of arrayn=len(arr)ans=_sum(arr)# display sumprint('Sum of the array is ',ans)

Output
('Sum of the array is ', 34)

Time complexity : O(n)
Auxiliary Space : O(1)

Python Program to Find Sum of Array Using enumerate function 

This code calculates the sum of elements in the list list1 using a loop. It iterates through each element, adds it to the running sum s, and then prints the final sum.

Python3
list1=[12,3,4,15];s=0fori,ainenumerate(list1):s+=aprint(s)

Output
34

Time complexity: O(n)
Auxiliary Space: O(1)

Python Program to Find Sum of Array Using Divide and conquer

Define a function that takes an array and two indices low and high. If low == high, return the element at that index. Calculate the midpoint of the array as mid = (low + high) // 2.Recursively find the sum of the left subarray as left_sum = sum_of_array(arr, low, mid). Recursively find the sum of the right subarray as right_sum = sum_of_array(arr, mid+1, high). Return the sum of the left and right subarrays as left_sum + right_sum.

Python3
defsum_of_array(arr,low,high):iflow==high:returnarr[low]mid=(low+high)//2left_sum=sum_of_array(arr,low,mid)right_sum=sum_of_array(arr,mid+1,high)returnleft_sum+right_sum#Examplesarr=[1,2,3]print(sum_of_array(arr,0,len(arr)-1))# Output: 6arr=[15,12,13,10]print(sum_of_array(arr,0,len(arr)-1))# Output: 50

Output
650

Please refer complete article onProgram to find sum of elements in a given array for more details!

Python Program to Find Sum of Array Using counter method.

This program finds the sum of an array using the Counter class from the collections module in Python. The Counter class is used to count the occurrences of elements in the input array. The program then iterates over the items in the counter and calculates the sum of the array.

Python3
fromcollectionsimportCounterarr=[12,3,4,15]c=Counter(arr)sum=0forkey,valueinc.items():sum+=key*valueprint("Sum of the array is",sum)

Output
Sum of the array is 34

Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp