![]() |
VOOZH | about |
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
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]
Final sum = 34
34
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.
34
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.
34