VOOZH about

URL: https://www.geeksforgeeks.org/dsa/mean-of-array-using-recursion/

⇱ Mean of array using recursion - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Mean of array using recursion

Last Updated : 19 Feb, 2026

Given an array arr[] of integers, 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: arr[] = [1, 2, 3, 4, 5]
Output: 3
Explanation: The sum of elements (15) divided by the number of elements (5) gives the mean: 3

Input: arr[] = [1, 2, 3]
Output: 2
Explanation: The sum of elements (6) divided by the number of elements (3) gives the mean: 2

[Approach] - Using Recursion - O(n) Time and O(n) Space

The idea is to compute the mean recursively by building it from smaller prefixes of the array.
Let mean(arr, n) represent the mean of the first n elements. We first calculate the mean of the first nāˆ’1 elements recursively. Since the mean of nāˆ’1 elements equals their sum divided by (nāˆ’1), multiplying it by (nāˆ’1) gives their total sum. We then add the nth element arr[nāˆ’1] to this sum and divide the result by n to obtain the mean of the first n elements.

The recursive formula for calculating the mean of an array is:


Output
3
Comment
Article Tags: