Mean of array using recursion Last Updated : 22 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Given an array of numbers, you are required to calculate the mean (average) using recursion. Note: The mean of an array is the sum of its elements divided by the number of elements in the array.Examples: Input: 1 2 3 4 5Output: 3Explanation: The sum of elements (15) divided by the number of elements (5) gives the mean: 3Input: 1 2 3Output: 2Explanation: The sum of elements (6) divided by the number of elements (3) gives the mean: 2The approach to finding the mean using recursion involves summing the elements of the array progressively. In each recursive call, the function calculates the sum of the first n-1 elements and adds the current element. It then divides the total sum by n to compute the mean. The base case is when there's only one element left, at which point that element is directly returned. The recursive formula for calculating the mean of an array is:\text{mean}(A, N) = \frac{\text{mean}(A, N-1) \times (N-1) + A[N-1]}{N} C++ #include <iostream> #include <vector> using namespace std; double findMean(const vector<int>& arr) { int n = arr.size(); if (n == 1) // Base case: when there is only one element return (double)arr[n-1]; else return ((double)(findMean(vector<int>(arr.begin(), arr.begin() + n-1)) * (n-1) + arr[n-1]) / n); } int main() { double mean = 0; vector<int> arr = {1, 2, 3, 4, 5}; cout << findMean(arr) << endl; return 0; } C #include <stdio.h> #include <stdlib.h> double findMean(int* arr, int n) { if (n == 1) // Base case: when there is only one element return (double)arr[n-1]; else return ((double)(findMean(arr, n-1) * (n-1) + arr[n-1]) / n); } int main() { double mean = 0; int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); printf("%f\n", findMean(arr, n)); return 0; } Java import java.util.Arrays; class GfG { static double findMean(int[] arr) { int n = arr.length; if (n == 1) // Base case: when there is only one element return (double)arr[n-1]; else return ((findMean(Arrays.copyOf(arr, n-1)) * (n-1) + arr[n-1]) / n); } public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; System.out.println(findMean(arr)); } } Python def findMean(arr): n = len(arr) if n == 1: # Base case: when there is only one element return float(arr[n - 1]) else: return (findMean(arr[:n - 1]) * (n - 1) + arr[n - 1]) / n if __name__ == "__main__": arr = [1, 2, 3, 4, 5] mean = findMean(arr) print("Mean:", mean) C# using System; class GfG { static double findMean(int[] arr) { int n = arr.Length; if (n == 1) // Base case: when there is only one element return (double)arr[n-1]; else { // Create a subarray excluding the last element int[] subArray = new int[n - 1]; Array.Copy(arr, subArray, n - 1); // Recursive case: calculate mean of the subarray and add // the current element return (findMean(subArray) * (n - 1) + arr[n - 1]) / n; } } static void Main() { int[] arr = {1, 2, 3, 4, 5}; Console.WriteLine(findMean(arr)); } } JavaScript function findMean(arr) { const n = arr.length; if (n === 1) // Base case: when there is only one element return arr[n-1]; else return (findMean(arr.slice(0, n-1)) * (n-1) + arr[n-1]) / n; } // Driver Code const arr = [1, 2, 3, 4, 5]; console.log(findMean(arr)); Output3 Time Complexity: O(n)Auxiliary Space: O(n) Comment More infoAdvertise with us Next Article Mean of array using recursion P Prakhar Agrawal Improve Article Tags : DSA Arrays Basic Coding Problems Practice Tags : Arrays Similar Reads Introduction to Recursion The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution 14 min read What is Recursion? Recursion is defined as a process which calls itself directly or indirectly and the corresponding function is called a recursive function.Example 1 : Sum of Natural Numbers Let us consider a problem to find the sum of natural numbers, there are several ways of doing that but the simplest approach is 8 min read Difference between Recursion and Iteration A program is called recursive when an entity calls itself. A program is called iterative when there is a loop (or repetition).Example: Program to find the factorial of a number C++ // C++ program to find factorial of given number #include<bits/stdc++.h> using namespace std; // ----- Recursion 6 min read Types of Recursions What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. Using recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inord 15+ min read Finite and Infinite Recursion with examples The process in which a function calls itself directly or indirectly is called Recursion and the corresponding function is called a Recursive function. Using Recursion, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Tr 6 min read What is Tail Recursion Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call.For example the following function print() is tail recursive.C++// An example of tail recursive funct 7 min read What is Implicit recursion? What is Recursion? Recursion is a programming approach where a function repeats an action by calling itself, either directly or indirectly. This enables the function to continue performing the action until a particular condition is satisfied, such as when a particular value is reached or another con 5 min read Why is Tail Recursion optimization faster than normal Recursion? What is tail recursion? Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call. What is non-tail recursion? Non-tail or head recursion is defined as a recur 4 min read Recursive Functions A Recursive function can be defined as a routine that calls itself directly or indirectly. In other words, a recursive function is a function that solves a problem by solving smaller instances of the same problem. This technique is commonly used in programming to solve problems that can be broken do 4 min read Difference Between Recursion and Induction Recursion and induction are fundamental ideas in computer science and mathematics that might be regularly used to solve problems regarding repetitive structures. Recursion is a programming technique in which a function calls itself to solve the problem, whilst induction is a mathematical proof techn 4 min read Like