VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-find-sum-elements-given-array/

⇱ Sum of Array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Sum of Array

Last Updated : 31 Mar, 2026

Given an array of integers arr[], 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

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


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.

👁 dsa1

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.


Output
34
Comment