Given an array of integers arr[], find the sum of its elements.
Examples:
Input : arr[] = [1, 2, 3]
Output : 6
Explanation: 1 + 2 + 3 = 6Input : arr[] = [15, 12, 13, 10]
Output : 50
Table of Content
Iterative Solution - O(n) Time and O(1) Space
Iterate through each element of the array and add it to a variable called sum. Initialize the sum variable to 0 before starting the iteration. After completing the iteration, return the final value of sum.
Step-by-step execution:
For 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;
}
import java.io.*;
class GFG {
static int arr[] = { 12, 3, 4, 15 };
static int arraySum()
{
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)
{
System.out.println(arraySum());
}
}
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 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;
}
public static void Main()
{
int[] arr = { 12, 3, 4, 15 };
int n = arr.Length;
Console.Write(arraySum(arr, n));
}
}
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 Solution - O(n) Time and O(n) Space
Use recursion to compute the sum by reducing the problem size in each call. In the base case, when the array size becomes 0, return 0. In the recursive case, return the element at index n - 1 added to the result of a recursive call with the size reduced by one, continuing until all elements are processed.

#include <iostream>
#include <vector>
using namespace std;
int arraySum(vector<int>& arr, int n)
{
// base case
if (n == 0) {
return 0;
}
else {
// recursively calling the function
return arr[n - 1] + arraySum(arr, n - 1);
}
}
int main()
{
vector<int> arr = { 12, 3, 4, 15 };
int n = arr.size();
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;
}
import java.io.*;
class GFG {
static int arraySum(int[] arr, int n)
{
// base case
if (n <= 0) {
return 0;
}
// recursively calling the function
return arraySum(arr, n - 1) + arr[n - 1];
}
public static void main(String[] args)
{
int arr[] = { 12, 3, 4, 15 };
int s = arraySum(arr, arr.length);
System.out.println(s);
}
}
def arraySum(arr):
# base case
if len(arr) == 0:
return 0
# recursively calling the function
return arr[0] + arraySum(arr[1:])
if __name__ == "__main__":
arr = [12, 3, 4, 15]
print(arraySum(arr))
using System;
public class GFG {
static int arraySum(int[] arr, int n)
{
// base case
if (n <= 0) {
return 0;
}
// recursively calling the function
return arraySum(arr, n - 1) + arr[n - 1];
}
public static void Main()
{
int[] arr = { 12, 3, 4, 15 };
int sum = arraySum(arr, arr.Length);
Console.Write(sum);
}
}
function arraySum(arr, n)
{
// base case
if (n <= 0) {
return 0;
}
// recursively calling the function
return arraySum(arr, n - 1) + arr[n - 1];
}
// Driver code
let arr = [12, 3, 4, 15];
let sum = arraySum(arr, arr.length);
console.log(sum);
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 main()
{
vector<int> arr = { 12, 3, 4, 15 };
// calling accumulate function to compute sum of elements
cout << accumulate(arr.begin(), arr.end(), 0);
return 0;
}
import java.util.Arrays;
public class GFG {
public static void main(String[] args) {
int[] arr = {12, 3, 4, 15};
// using stream to compute sum of elements
int sum = Arrays.stream(arr).sum();
System.out.println(sum);
}
}
if __name__ == "__main__":
arr = [12, 3, 4, 15]
# calling sum function to compute sum of elements
print(sum(arr))
using System;
using System.Linq;
class GFG {
public static void Main()
{
int[] arr = { 12, 3, 4, 15 };
// calling LINQ Sum method to compute sum of elements
int sum = arr.Sum();
Console.Write(sum);
}
}
const arr = [12, 3, 4, 15];
// calling reduce function to compute sum of elements
const sum = arr.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum);
Output
34