# Simple Array Sum

**Introduction**

The summation of numbers is the most over looked algorithm but it lays the foundation of iteration and aggregation algorithms. This little problem is simple and so is it’s solution.

**Problem Statement**

Below are the requirements and constraints to solving this problem as defined by Hackerrank.

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

For example, if the array

ar =[1,2,3], 1+2+3 = 6

so return 6.

**Function Description**

Complete the *simpleArraySum* function in the editor below. It must return the sum of the array elements as an integer.

simpleArraySum has the following parameter(s):

*ar*: an array of integers

**Input Format**

The first line contains an integer,

0<n, ar[i]≤ 1000 denoting the size of the array.

The second line contains space-separated integers representing the array’s elements.

**Constraints**

**Output Format**

Print the sum of the array’s elements as a single integer.

**Sample Input**

`6`

1 2 3 4 10 11

**Sample Output**

`31`

**Explanation**

We print the sum of the array’s elements: 1 + 2 + 3 + 4 + 10 + 11 = 31

**Solution**

defsimpleArraySum(ar):“””Initialize sum to zeroNote:The variable name for the sum is my_sum and not sum.This is due to the fact that sum is a keyword that references an inbuilt python function which will pretty much do what were doing here but it’s important to understand the process.“””my_sum = 0“””We are going to iterate through the array to and add every value in the array to the current sum.Note:since the sum is initialized to zero the value of sum will just be the total of all the values in the array . Consider it the offset. Like you would if you were counting on a number line.“””fornumberinar:“””The += operator is simply an augmented operator that equivalent to the summation of the value on the right side and the left side of the operator in simple terms. An example in this case it will translate to.my_sum = my_sum + number.“””my_sum += number“””simply return the sum after the iteration.To complete the challenge.“””returnmy_sum"""

time complexity = O(n)

space complexity = O(1)"""

**Conclusion**

While the solution may be python specific the idea of how to go about it completely language agnostic. This is to say that you may apply this algorithm to any language if you understand the concept well enough. Just as a side note it is possible to solve this problem with one line of code. This is done by using the in built sum method in python.

Example:

defsimpleArraySum(ar):returnsum(ar)

This neat one-liner can save you a lot of code and is more readable to more experienced developers. You could do it either way, but it’s important to understand the underlying concept.