Skip to content
Related Articles

Related Articles

Improve Article

Tail recursion to calculate sum of array elements.

  • Difficulty Level : Easy
  • Last Updated : 14 May, 2021

Given an array A[], we need to find the sum of its elements using Tail Recursion Method. We generally want to achieve tail recursion (a recursive function where recursive call is the last thing that function does) so that compilers can optimize the code. Basically, if recursive call is the last statement, the compiler does not need to save the state of parent call. 
Examples: 
 

Input : A[] = {1, 8, 9}
Output : 18

Input : A[] = {2, 55, 1, 7}
Output : 65

For Linear Recursion Method, refer: https://www.geeksforgeeks.org/sum-array-elements-using-recursion/
Logic: Here the key to tail recursion is whatever operation is applied with the function call, maintain it as a separate function parameter. 
So, keep the sum of the last elements K elements as a function parameter and return sum when K=0.
 

C++




#include <bits/stdc++.h>
using namespace std;
 
// Tail recursive function
int arrSum(int* array, int size, int sum = 0)
{
    // Base Case
    if (size == 0)
        return sum;
 
    // Function Call Observe sum+array[size-1]
    // to maintain sum of elements
    return arrSum(array, size - 1, sum + array[size - 1]);
}
 
int main()
{
    int array[] = { 2, 55, 1, 7 };
    int size = sizeof(array) / sizeof(array[0]);
    cout << arrSum(array, size);
    return 0;
}

Java




// Java implementation of the given approach.
class GFG
{
 
// Tail recursive function
static int arrSum(int []array, int size, int sum)
{
    // Base Case
    if (size == 0)
        return sum;
 
    // Function Call Observe sum+array[size-1]
    // to maintain sum of elements
    return arrSum(array, size - 1, sum + array[size - 1]);
}
 
// Driver code
public static void main(String[] args)
{
    int array[] = { 2, 55, 1, 7 };
    int size = array.length;
    System.out.print(arrSum(array, size, 0));
}
}
 
// This code is contributed by Rajput-Ji

Python3




# Python3 implementation of the given approach.
 
# Tail recursive function
def arrSum(array, size, Sum = 0):
 
    # Base Case
    if size == 0:
        return Sum
 
    # Function Call Observe sum+array[size-1]
    # to maintain sum of elements
    return arrSum(array, size - 1,
            Sum + array[size - 1])
 
# Driver Code
if __name__ == "__main__":
 
    array = [2, 55, 1, 7]
    size = len(array)
    print(arrSum(array, size))
 
# This code is contributed by Rituraj Jain

C#




     
// C# implementation of the given approach.
using System;
 
class GFG
{
 
// Tail recursive function
static int arrSum(int []array, int size, int sum)
{
    // Base Case
    if (size == 0)
        return sum;
 
    // Function Call Observe sum+array[size-1]
    // to maintain sum of elements
    return arrSum(array, size - 1, sum + array[size - 1]);
}
 
// Driver code
public static void Main(String[] args)
{
    int []array = { 2, 55, 1, 7 };
    int size = array.Length;
    Console.WriteLine(arrSum(array, size, 0));
}
}
 
// This code is contributed by 29AjayKumar

Javascript




<script>
 
// Tail recursive function
function arrSum(array, size, sum = 0)
{
    // Base Case
    if (size == 0)
        return sum;
 
    // Function Call Observe sum+array[size-1]
    // to maintain sum of elements
    return arrSum(array, size - 1, sum + array[size - 1]);
}
 
var array = [2, 55, 1, 7];
var size = array.length;
document.write( arrSum(array, size));
 
</script>   
Output: 
65

 

Time Complexity: O(n)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :