Mean of array using recursion (original) (raw)

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:

\text{mean}(arr, n) = \frac{\text{mean}(arr, n-1) \times (n-1) + arr[n-1]}{n}

C++ `

//Driver Code Starts #include #include using namespace std;

//Driver Code Ends

// helper recursive function double findMeanRec(vector& arr, int n) { // Base case if (n == 1) return arr[0];

// Recursive case
return (findMeanRec(arr, n - 1) * (n - 1) + arr[n - 1]) / n;

}

double findMean(vector& arr) { return findMeanRec(arr, arr.size()); }

//Driver Code Starts

int main() { vector arr = {1, 2, 3, 4, 5}; cout << findMean(arr); return 0; } //Driver Code Ends

C

//Driver Code Starts #include <stdio.h>

//Driver Code Ends

// helper recursive function double findMeanRec(int arr[], int n){ // Base case if (n == 1) return arr[0];

// Recursive case
return (findMeanRec(arr, n - 1) * (n - 1) + arr[n - 1]) / n;

}

double findMean(int arr[], int n){ return findMeanRec(arr, n); }

//Driver Code Starts int main(){ int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); printf("%f", findMean(arr, n)); return 0; } //Driver Code Ends

Java

//Driver Code Starts import java.util.*;

//Driver Code Ends

class GfG {

// helper recursive function static double findMeanRec(int[] arr, int n){ // Base case if (n == 1) return arr[0];

// Recursive case
return (findMeanRec(arr, n - 1) * (n - 1) + arr[n - 1]) / n;

}

static double findMean(int[] arr){ return findMeanRec(arr, arr.length); }

public static void main(String[] args){ int[] arr = {1, 2, 3, 4, 5}; System.out.println(findMean(arr));} }

Python

helper recursive function

def findMeanRec(arr, n): # Base case if n == 1: return arr[0]

# Recursive case
return (findMeanRec(arr, n - 1) * (n - 1) + arr[n - 1]) / n

def findMean(arr): return findMeanRec(arr, len(arr))

if name == "main": arr = [1, 2, 3, 4, 5] print(findMean(arr))

C#

//Driver Code Starts using System;

//Driver Code Ends

class GfG{ // helper recursive function static double findMeanRec(int[] arr, int n){ // Base case if (n == 1) return arr[0];

    // Recursive case
    return (findMeanRec(arr, n - 1) * (n - 1) + arr[n - 1]) / n;
}

static double findMean(int[] arr){
    return findMeanRec(arr, arr.Length);
}

//Driver Code Starts

static void Main(){
    int[] arr = {1, 2, 3, 4, 5};
    Console.WriteLine(findMean(arr));
}

} //Driver Code Ends

JavaScript

// helper recursive function function findMeanRec(arr, n) { // Base case if (n === 1) return arr[0];

// Recursive case
return (findMeanRec(arr, n - 1) * (n - 1) + arr[n - 1]) / n;

}

function findMean(arr) { return findMeanRec(arr, arr.length); } // Driver Code let arr = [1, 2, 3, 4, 5]; console.log(findMean(arr));

`