Find mean of subarray means in a given array
Last Updated :
09 Dec, 2022
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
Naive approach: A simple solution is to consider all subarrays of size m, compute their means. Finally, return mean of means.
An efficient solution is to 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 <bits/stdc++.h>
using namespace std;
// function to find mean value
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[0]);
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 value
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
<?php
// PHP program to find mean of means
// function to find mean value
function findMean($arr, $n, $m)
{
// declare sum and
// winSum (window sum)
$sum = 0;
$winSum = 0;
$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
$arr = array(2, 5, 7, 1, 9, 3, 9);
$n =count($arr);
$m = 4;
echo "Mean = ", findMean($arr, $n, $m);
// This code is contributed by anuj_67
?>
JavaScript
<script>
// Javascript program to find mean of means
// function to find mean value
function findMean(arr, n, m)
{
// declare sum and winSum (window sum)
var sum = 0, winSum = 0;
var 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
var arr = [ 2, 5, 7, 1, 9, 3, 9 ];
var n = arr.length;
var m = 4;
document.write( "Mean = " + findMean(arr, n, m));
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Find sum of count of duplicate numbers in all subarrays of given array Given an array arr[] of size N. The task it to find the sum of count of duplicate numbers in all subarrays of given array arr[]. For example array {1, 2, 3, 2, 3, 2} has two duplicate elements (i.e, 2 and 3 come more than one time in the array). Examples:Input: N = 2, arr = {3,3}Output: 1Explanation
6 min read
Count of Subarrays of given Array with median at least X Given an array arr[]of integers with length N and an integer X, the task is to calculate the number of subarrays with median greater than or equal to the given integer X. Examples: Input: N=4, A = [5, 2, 4, 1], X = 4Output: 7Explanation: For subarray [5], median is 5. (>= 4)For subarray [5, 2], m
9 min read
Find the missing integer in an array if mean is given Given an array of size N-1 and the mean of N elements (one element is not given). We need to find the missing value X in the array. Examples: Input : a[] = {2, 4, 20}, Mean = 9Output : Missing Element = 10 Explanation : Mean of (2, 4, 20, 10) is (2 + 4 + 20 + 10)/4 = 9 Let x be the missing element M
4 min read
Find all subarrays with sum in the given range Given an unsorted array of size, N. Find subarrays that add to a sum in the given range L-R. Examples: Input: arr[] = {2, 3, 5, 8}, L = 4, R = 13Output: The indexes of subarrays are {0, 1}, {0, 2}, {1, 2}, {2, 2}, {2, 3}, {3, 3} Input: arr[] = {1, 4, 6}, L = 3, R = 8Output: The indexes of subarrays
4 min read
Mean of range in array Given an array arr[] of n integers and q queries represented by an array queries[][], where queries[i][0] = l and queries[i][1] = r. For each query, the task is to calculate the mean of elements in the range l to r and return its floor value. Examples: Input: arr[] = [3, 7, 2, 8, 5] queries[][] = [[
12 min read
Print all subarrays with sum in a given range Given an array arr[] of positive integers and two integers L and R defining the range [L, R]. The task is to print the subarrays having sum in the range L to R. Examples: Input: arr[] = {1, 4, 6}, L = 3, R = 8Output: {1, 4}, {4}, {6}.Explanation: All the possible subarrays are the following{1] with
5 min read
Generate a Matrix with mean of each subarray of each row as an integer Given two integers M and N, the task is to generate an MxN matrix having elements in range [1, MxN] such that the average of any subarray of any row is an integer. If it is not possible to do so, return -1. Examples: Input: M = 2, N = 3Output: 1 3 5 2 4 6 Explanation: Subarrays of first row with siz
8 min read
Maximize sum of means of two subsets formed by dividing given Array into them Given an array arr[] of size N, the task is to find the maximum sum of means of 2 non-empty subsets of the given array such that each element is part of one of the subsets. Examples: Input: N = 2, arr[] = {1, 3}Output: 4.00Explanation: Since there are only two elements, make two subsets as {1} and {
5 min read
Find array elements equal to sum of any subarray of at least size 2 Given an array arr[], the task is to find the elements from the array which are equal to the sum of any sub-array of size greater than 1.Examples: Input: arr[] = {1, 2, 3, 4, 5, 6} Output: 3, 5, 6 Explanation: The elements 3, 5, 6 are equal to sum of subarrays {1, 2},{2, 3} and {1, 2, 3} respectivel
6 min read
Find a subarray whose sum is divisible by size of the array Given an array arr[] of length N. The task is to check if there exists any subarray whose sum is a multiple of N. If there exists such subarray, then print the starting and ending index of that subarray else print -1. If there are multiple such subarrays, print any of them. Examples: Input: arr[] =
13 min read