![]() |
VOOZH | about |
Given an array arr, we need to find the sum of its elements using Tail Recursion Method. We generally want to achieve tail recursion so that compilers can optimize the code. If the recursive call is the last statement, the compiler can optimize it by eliminating the need to store the parent call's state.
Examples:
Input: arr = [1, 8, 9]
Output: 18
Explanation: The sum of the elements in the array [1, 8, 9] is 18.
Input: arr = [2, 55, 1, 7]
Output: 65
For the Normal Recursion Method, check out this guide: Sum of Array Elements Using Recursion
arr[size - 1] and add it to the sum. size - 1 (excluding the last element).size until it reaches 0.65
Time Complexity: O(n), where n is the length of array.
Auxiliary Space: O(n), due to recursive function calls stored in the call stack.