Given an array of integers arr[], find the sum of all its elements.
Examples:
Input : arr[] = [1, 2, 3]
Output : 6
Explanation: 1 + 2 + 3 = 6Input : arr[] = [15, 12, 13, 10]
Output : 50
Explanation: 15 + 12 + 13 + 10 = 50
Try It Yourself
Table of Content
Iterative Approch - O(n) Time and O(1) Space
Initialize a variable sum to 0 and traverse the array. Add each element to sum. After processing all elements, return sum.
Consider arr[] = [12, 3, 4, 15]
- Initialize : sum = 0
- i = 0, sum = 0 + 12 = 12
- i = 1, sum = 12 + 3 = 15
- i = 2, sum = 15 + 4 = 19
- i = 3, sum = 19 + 15 = 34
Final sum = 34
#include <iostream>
#include <vector>
using namespace std;
int arraySum(vector<int>& arr) {
int sum = 0;
// Iterate through all elements and add them to sum
for (int i = 0; i < arr.size(); i++)
sum += arr[i];
return sum;
}
int main()
{
vector<int> arr = {12, 3, 4, 15};
cout << arraySum(arr);
return 0;
}
#include <stdio.h>
int arraySum(int arr[], int n)
{
int sum = 0;
// Iterate through all elements and add them to sum
for (int i = 0; i < n; i++)
sum += arr[i];
return sum;
}
int main()
{
int arr[] = { 12, 3, 4, 15 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d", arraySum(arr, n));
return 0;
}
public class GFG {
static int arraySum(int[] arr) {
int sum = 0;
// Iterate through all elements and add them to sum
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
public static void main(String[] args) {
int[] arr = {12, 3, 4, 15};
System.out.println(arraySum(arr));
}
}
def arraySum(arr):
sum = 0
# Iterate through all elements and add them to sum
for num in arr:
sum += num
return sum
if __name__ == "__main__":
arr = [12, 3, 4, 15]
print(arraySum(arr))
using System;
class GFG {
static int arraySum(int[] arr) {
int sum = 0;
// Iterate through all elements and add them to sum
for (int i = 0; i < arr.Length; i++) {
sum += arr[i];
}
return sum;
}
static void Main() {
int[] arr = { 12, 3, 4, 15 };
Console.WriteLine(arraySum(arr));
}
}
function arraySum(arr) {
let sum = 0;
// Iterate through all elements and add them to sum
for (let i = 0; i < arr.length; i++)
sum += arr[i];
return sum;
}
// Driver code
let arr = [12, 3, 4, 15];
console.log(arraySum(arr));
Output
34
Recursive Approach - O(n) Time and O(n) Space
The recursive approach calculates the sum of array elements by calling the same function repeatedly.
- Start from the first element of the array.
- Add the current element to the sum returned by the recursive function for the remaining elements.
- When the index reaches the array length, return 0 to stop the recursion.

#include <iostream>
#include <vector>
using namespace std;
int sumHelper(const vector<int>& arr, int index) {
// Base Case: when index reaches the end of the list
if (index == arr.size()) {
return 0;
}
// Current element + remaining elements ka recursive sum
return arr[index] + sumHelper(arr, index + 1);
}
int arraySum(vector<int>& arr) {
return sumHelper(arr, 0);
}
int main() {
vector<int> arr = {12, 3, 4, 15};
cout << arraySum(arr) << "\n";
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int arraySum(int arr[], int n)
{
// base case
if (n == 0) {
return 0;
}
else {
// recursively calling the function
return arr[0] + arraySum(arr + 1, n - 1);
}
}
int main()
{
int arr[] = { 12, 3, 4, 15 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d", arraySum(arr, n));
return 0;
}
public class GFG {
private static int sumHelper(int[] arr, int index) {
// Base Case: when index reaches the end of the array
if (index == arr.length) {
return 0;
}
// Current element + recursive sum of remaining elements
return arr[index] + sumHelper(arr, index + 1);
}
public static int arraySum(int[] arr) {
return sumHelper(arr, 0);
}
public static void main(String[] args) {
int[] arr = {12, 3, 4, 15};
System.out.println(arraySum(arr));
}
}
def sumHelper(arr, index):
# Base Case: when index reaches the end of the list
if index == len(arr):
return 0
# Current element + recursive sum of remaining elements
return arr[index] + sumHelper(arr, index + 1)
def arraySum(arr):
return sumHelper(arr, 0)
if __name__ == "__main__":
arr = [12, 3, 4, 15]
print(arraySum(arr))
using System;
class GFG {
private static int sumHelper(int[] arr, int index) {
// Base Case: when index reaches the end of the array
if (index == arr.Length) {
return 0;
}
// Current element + recursive sum of remaining elements
return arr[index] + sumHelper(arr, index + 1);
}
public static int arraySum(int[] arr) {
return sumHelper(arr, 0);
}
static void Main() {
int[] arr = { 12, 3, 4, 15 };
Console.WriteLine(arraySum(arr));
}
}
function sumHelper(arr, index) {
// Base Case: when index reaches the end of the array
if (index === arr.length) {
return 0;
}
// Current element + recursive sum of remaining elements
return arr[index] + sumHelper(arr, index + 1);
}
function arraySum(arr) {
return sumHelper(arr, 0);
}
// Driver code
const arr = [12, 3, 4, 15];
console.log(arraySum(arr));
Output
34
Inbuilt Methods - O(n) Time and O(1) Space
Use built-in functions to compute the sum of elements in a given array. These functions eliminate the need for explicit iteration and improve code simplicity.
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int arraySum(vector<int>& arr) {
return accumulate(arr.begin(), arr.end(), 0);
}
int main() {
vector<int> arr = {12, 3, 4, 15};
cout << arraySum(arr) << "\n";
return 0;
}
import java.util.Arrays;
public class GFG {
public static int arraySum(int[] arr) {
return Arrays.stream(arr).sum();
}
public static void main(String[] args) {
int[] arr = { 12, 3, 4, 15 };
System.out.println(arraySum(arr));
}
}
def arraySum(arr):
return sum(arr)
if __name__ == "__main__":
arr = [12, 3, 4, 15]
print(arraySum(arr))
using System;
using System.Linq;
class GFG {
static int arraySum(int[] arr) {
return arr.Sum();
}
static void Main() {
int[] arr = { 12, 3, 4, 15 };
Console.WriteLine(arraySum(arr));
}
}
function arraySum(arr) {
return arr.reduce((acc, curr) => acc + curr, 0);
}
// Driver code
const arr = [12, 3, 4, 15];
console.log(arraySum(arr));
Output
34