 GeeksforGeeks App
Open App Browser
Continue

# Python Program to find sum of array

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

Examples:

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

Input : arr[] = {15, 12, 13, 10}
Output : 50

Method 1: 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 array`  `def` `_sum(arr):` `    ``# initialize a variable``    ``# to store the sum``    ``# while iterating through``    ``# the array later``    ``sum` `=` `0` `    ``# iterate through the array``    ``# and add each element to the sum variable``    ``# one at a time``    ``for` `i ``in` `arr:``        ``sum` `=` `sum` `+` `i` `    ``return``(``sum``)`  `# main function``if` `__name__ ``=``=` `"__main__"``:``    ``# input values to list``    ``arr ``=` `[``12``, ``3``, ``4``, ``15``]` `    ``# calculating length of array``    ``n ``=` `len``(arr)``    ``# calling function ans store the sum in ans``    ``ans ``=` `_sum(arr)``    ``# display sum``    ``print``(``'Sum of the array is '``, ans)`

Output

`Sum of the array is  34`

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

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

Syntax:

`sum(iterable) `

iterable: iterable can be anything list, tuples or dictionaries, but most importantly it should be numbered.

## Python3

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

Output

`Sum of the array is  34`

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

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

Syntax:

`reduce( function, Array );`

## Python

 `from` `functools ``import` `reduce``# Python 3 code to find sum``# of elements in given array`  `def` `_sum(arr):` `    ``# iterate over array``    ``# using reduce and get``    ``# sum on accumulator``    ``sum` `=` `reduce``(``lambda` `a, b: a``+``b, arr)` `    ``return``(``sum``)`  `# driver function``arr ``=` `[]``# input values to list``arr ``=` `[``12``, ``3``, ``4``, ``15``]` `# calculating length of array``n ``=` `len``(arr)` `ans ``=` `_sum(arr)` `# display sum``print``(``'Sum of the array is '``, ans)`

Output

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

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

Example: Using enumerate function

## Python3

 `list1 ``=` `[``12``, ``3``, ``4``, ``15``];s``=``0``for` `i,a ``in` `enumerate``(list1):``  ``s``+``=``a``print``(s)`

Output

`34`

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

### Approach: Divide and conquer

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

## Python3

 `def` `sum_of_array(arr, low, high):``    ``if` `low ``=``=` `high:``        ``return` `arr[low]``    ``mid ``=` `(low ``+` `high) ``/``/` `2``    ``left_sum ``=` `sum_of_array(arr, low, mid)``    ``right_sum ``=` `sum_of_array(arr, mid``+``1``, high)``    ``return` `left_sum ``+` `right_sum``#Examples``arr ``=` `[``1``, ``2``, ``3``]``print``(sum_of_array(arr, ``0``, ``len``(arr)``-``1``)) ``# Output: 6` `arr ``=` `[``15``, ``12``, ``13``, ``10``]``print``(sum_of_array(arr, ``0``, ``len``(arr)``-``1``)) ``# Output: 50`

Output

```6
50```

Time complexity: O(n log n), where n is the number of elements in the array.

Auxiliary space: O(log n), where n is the number of elements in the array.

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

METHOD 5:Using counter method.

APPROACH:

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.

ALGORITHM:

1.Import the collections module.
2.Initialize the input array arr.
3.Create a Counter object c from the input array.
4.Initialize a variable sum to 0.
5.Iterate over the items in c using the items() method.
6.Multiply the key (element) with its count (value) and add it to the sum variable.
7.Print the sum of the array.

## Python3

 `from` `collections ``import` `Counter` `arr ``=` `[``12``, ``3``, ``4``, ``15``]``c ``=` `Counter(arr)``sum` `=` `0` `for` `key, value ``in` `c.items():``    ``sum` `+``=` `key ``*` `value` `print``(``"Sum of the array is"``, ``sum``)`

Output

```Sum of the array is 34
```

Time Complexity:

The time complexity of this program is O(n) where n is the length of the input array. This is because we need to iterate over all the elements in the array once to create the Counter object and then iterate over the items in the counter once to calculate the sum.

Space Complexity:

The space complexity of this program is O(n) where n is the length of the input array. This is because we need to create a Counter object to store the counts of elements in the array, which can take up to n space in the worst case. We also need a constant amount of extra space for the sum variable and the loop variables.

My Personal Notes arrow_drop_up