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: 3Input: 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:
//Driver Code Starts
#include <iostream>
#include <vector>
using namespace std;
//Driver Code Ends
// helper recursive function
double findMeanRec(vector<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(vector<int>& arr) {
return findMeanRec(arr, arr.size());
}
//Driver Code Starts
int main() {
vector<int> arr = {1, 2, 3, 4, 5};
cout << findMean(arr);
return 0;
}
//Driver Code Ends
//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
//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));}
}
# 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))
//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
// 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));
Output
3