Given an integer array arr of size n, you need to sum the elements of arr.
Example 1:
Input:
n = 3
arr[] = {3 2 1}
Output: 6
Example 2:
Input:
n = 4
arr[] = {1 2 3 4}
Output: 10
Your Task:
You need to complete the function sumElement() that takes arr and n and returns the sum. The printing is done by the driver code.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 <= n <= 10^{3}
1 <= arr_{i }<= 10^{4}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3

def sumElement(arr,n):
    #code here

Solutions:
def sumElement(arr, n):
	sumElement = 0
	for i in arr:
		sumElement = sumElement + i
	return sumElement


def sumElement(arr, n):
	a = 0
	for i in range(0, len(arr)):
		a += arr[i]
	return a


def sumElement(arr, n):
	sum = 0
	for i in range(n):
		sum = sum + arr[i]
	return sum


def sumElement(arr, n):
	return sum(arr)


def sumElement(arr, n):
	count = 0
	for i in range(n):
		count += arr[i]
	return count


def sumElement(arr, n):
	s = 0
	for c in arr:
		s = s + c
	return s


def sumElement(arr, n):
	s = 0
	for ele in arr:
		s += ele
	return s


def sumElement(arr, n):
	s = 0
	for v in range(0, n):
		s += arr[v]
	return s


def sumElement(arr, n):
	s = 0
	for i in range(0, n):
		s += arr[i]
	return s


def sumElement(arr, n):
	import numpy as np
	return sum(arr)


def sumElement(arr, n):
	sum = 0
	for n in arr:
		sum += n
	return sum


def sumElement(arr, n):
	index = 0
	ans = 0
	for index in range(n):
		ans = ans + arr[index]
	return ans


def sumElement(arr, n):
	l = list(arr)
	return sum(l)


def sumElement(arr, n):
	add = 0
	for ele in arr:
		add += ele
	return add


def sumElement1(arr, n):
	sum = 0
	for i in range(len(arr)):
		sum += arr[i]
	return sum

def sumElement(arr, n):
	mid = len(arr) // 2
	j = len(arr) - 1
	sum1 = 0
	i = 0
	while i < mid and j >= mid:
		sum1 += arr[i] + arr[j]
		i += 1
		j -= 1
	if len(arr) % 2 == 0:
		return sum1
	else:
		return sum1 + arr[mid]


def sumElement(arr, n):
	total = 0
	for i in range(n):
		total += arr[i]
	return total


def sumElement(arr, n):
	if n == 0:
		return 0
	return arr[n - 1] + sumElement(arr, n - 1)