# Find mean of subarray means in a given array

You are given an array of n-elements you have to find the mean of the array as mean of all consecutive m-elements of array for all possible m-length array with consecutive elements.

Examples:

```Input :arr[] = {3, 5, 1, 8, 9, 4},
m = 4
Output : Mean = 5.16667
Explanation : {3, 5, 1, 8}, {5, 1, 8, 9},
{1, 8, 9, 4} are three set of m-consecu-
tive elements. Mean of mean of sets
is (17/4 + 23/4 + 22/4 )/ 3

Input : arr[] = {9, 4}, m = 1
Output : Mean = 6.5
Explanation : {9}, {4} are two set of
1-consecutive element. Mean of means
of sets is (9 + 4 )/ 2
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A simple solution is to consider all subarrays of size m, compute their means. Finally return mean of means.

An efficient solution is use the Sliding Window Algorithm for this problem where we find sum of m-length window and then add up mean of each window to a value sum. At last we will have our result by dividing total sum by number of window possible and that too is sum /(n-m+1).

## C++

 `// CPP program to find mean of means ` `#include ` `using` `namespace` `std; ` ` `  `// function to find mean vale ` `float` `findMean(``int` `arr[], ``int` `n, ``int` `m) ` `{ ` `    ``// declare sum and winSum (window sum) ` `    ``float` `sum = 0, winSum = 0; ` `    ``int` `i = 0; ` ` `  `    ``// find sum for 1st m-length window ` `    ``for` `(; i < m; i++) ` `        ``winSum += arr[i]; ` `    ``sum += (winSum / m); ` ` `  `    ``// iterate over array to find sum  ` `    ``// of all m-length means ` `    ``for` `(; i < n; i++) { ` `        ``winSum = winSum - arr[i - m] + arr[i]; ` `        ``sum += (winSum / m); ` `    ``} ` ` `  `    ``// mean of means will be sum of means  ` `    ``// divided by no of such means ` `    ``return` `(sum / (n - m + 1)); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 2, 5, 7, 1, 9, 3, 9 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``int` `m = 4; ` `    ``cout << ``"Mean = "` `<< findMean(arr, n, m); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find mean of means ` `import` `java.util.*; ` `import` `java.lang.*; ` ` `  `public` `class` `GeeksforGeeks{ ` `     `  `    ``// function to find mean value ` `    ``public` `static` `float` `findMean(``int` `arr[], ``int` `n, ` `                                          ``int` `m){ ` `     `  `        ``// declare sum and winSum (window sum) ` `        ``float` `sum = ``0``, winSum = ``0``; ` `        ``int` `i = ``0``; ` ` `  `        ``// find sum for 1st m-length window ` `        ``for` `(; i < m; i++) ` `            ``winSum += arr[i]; ` `        ``sum += (winSum / m); ` ` `  `        ``// iterate over array to find sum  ` `        ``// of all m-length means ` `        ``for` `(; i < n; i++) { ` `            ``winSum = winSum - arr[i - m] + arr[i]; ` `            ``sum += (winSum / m); ` `        ``} ` ` `  `        ``// mean of means will be sum of means  ` `        ``// divided by no of such means ` `        ``return` `(sum / (n - m + ``1``)); ` `    ``} ` `     `  `    ``// driver code ` `    ``public` `static` `void` `main(String argc[]){ ` `    ``int` `arr[] = { ``2``, ``5``, ``7``, ``1``, ``9``, ``3``, ``9` `}; ` `    ``int` `n = ``7``; ` `    ``int` `m = ``4``; ` `    ``System.out.println(``"Mean = "` `+  ` `                      ``findMean(arr, n, m)); ` `    ``} ` `} ` ` `  `/*This code is contributed by Sagar Shukla.*/`

## Python3

 `# Python3 program to find mean of means ` ` `  `# function to find mean vale ` `def` `findMean(arr, n, m) : ` `     `  `    ``# declare sum and winSum (window sum) ` `    ``sum` `=` `float``(``0``) ` `    ``winSum ``=` `float``(``0``) ` `    ``i ``=` `0` ` `  `    ``# find sum for 1st m-length window ` `    ``while` `(i < m): ` `        ``winSum ``=` `winSum ``+` `arr[i] ` `        ``i ``=` `i ``+` `1` `    ``sum` `=` `sum` `+` `(winSum ``/` `m); ` ` `  `    ``# iterate over array to find sum  ` `    ``# of all m-length means ` `    ``while` `(i < n): ` `        ``winSum ``=` `winSum ``-` `arr[i ``-` `m] ``+` `arr[i] ` `        ``sum` `=` `sum` `+` `(winSum ``/` `m) ` `        ``i ``=` `i ``+` `1` ` `  `    ``# mean of means will be sum of means  ` `    ``# divided by no of such means ` `    ``return` `(``sum` `/` `(n ``-` `m ``+` `1``)); ` ` `  `# Driven code ` `arr ``=` `[ ``2``, ``5``, ``7``, ``1``, ``9``, ``3``, ``9` `] ` `n ``=` `len``(arr) ` `m ``=` `4` `print` `(``"Mean = "``, findMean(arr, n, m)) ` ` `  `# This code is contributed by "rishabh_jain". `

## C#

 `// Java program to find mean of means ` `using` `System; ` ` `  `public` `class` `GeeksforGeeks{ ` `     `  `    ``// function to find mean value ` `    ``public` `static` `float` `findMean(``int` `[]arr, ``int` `n, ` `                                            ``int` `m) ` `    ``{ ` `     `  `        ``// declare sum and winSum (window sum) ` `        ``float` `sum = 0, winSum = 0; ` `        ``int` `i = 0; ` ` `  `        ``// find sum for 1st m-length window ` `        ``for` `(; i < m; i++) ` `            ``winSum += arr[i]; ` `             `  `        ``sum += (winSum / m); ` ` `  `        ``// iterate over array to find sum  ` `        ``// of all m-length means ` `        ``for` `(; i < n; i++) { ` `            ``winSum = winSum - arr[i - m] + arr[i]; ` `             `  `            ``sum += (winSum / m); ` `        ``} ` ` `  `        ``// mean of means will be sum of means  ` `        ``// divided by no of such means ` `        ``return` `(sum / (n - m + 1)); ` `    ``} ` `     `  `    ``// driver code ` `    ``public` `static` `void` `Main(){ ` `    ``int` `[]arr = { 2, 5, 7, 1, 9, 3, 9 }; ` `    ``int` `n = 7; ` `    ``int` `m = 4; ` `     `  `    ``Console.WriteLine(``"Mean = "` `+  ` `                    ``findMean(arr, n, m)); ` `    ``} ` `} ` ` `  `/* This code is contributed by vt_m.*/`

## PHP

 ` `

Output:

```Mean = 4.9375
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : vt_m

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.