
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find mean of subarray means in a given array in C++
In this problem, we are given an array arr[] of size n and an integer m. Our task is to Find mean of subarray means in a given array.
Code Description − Here, we need to find the mean of array as the mean of means of subarray of size m.
Let’s take an example to understand the problem,
Input
arr[] = {2, 5, 3, 6, 1}, m = 3
Output
3.78
Explanation
All subarrays of size m are {2, 5, 3}, {5, 3, 6}, {3, 6, 1} Means of means of subarray of size m,
$$(\left(\frac{2+5+3}{3}\right)+\left(\frac{5+3+6}{3}\right)+\left(\frac{3+6+1}{3}\right))/3=\left(\frac{10}{3}\right)+\left(\frac{14}{3}\right)+\left(\frac{10}{3}\right)/3=34/3/3=3.78$$
Solution Approach
A simple solution to the problem is by finding all subarrays of size m and finding their means. Then add all these means and divide it by the count of subarrays. And return the result.
Another more efficient approach is by using the sliding window algorithm. We will find a finding of size m starting from index 0. For each window find the mean and sum. And at the end divide the sum by window count and return the value.
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; float calcMeanOfSubarrayMeans(int arr[], int n, int m) { float meanSum = 0, windowSum = 0; for (int i = 0; i < m; i++) windowSum += arr[i]; meanSum += (windowSum / m); for (int i = 0; i < n; i++) { windowSum = windowSum - arr[i - m] + arr[i]; meanSum += (windowSum / m); } int windowCount = n - m + 1; return (meanSum / windowCount); } int main() { int arr[] = { 4, 1, 7, 9, 2, 5, 3}; int n = sizeof(arr) / sizeof(arr[0]); int m = 3; cout<<"The mean of subarray means is "<<calcMeanOfSubarrayMeans(arr, n, m); return 0; }
Output
The mean of subarray means is 8.06667
- Related Articles
- Find Sum of all unique subarray sum for a given array in C++
- Program to find sum of the 2 power sum of all subarray sums of a given array in Python
- Find Elements Greater Than a Given Number In a Subarray in Java
- Find Mean of a List of Numpy Array in Python
- Program to find out the greatest subarray of a given length in python
- Find subarray with given sum - (Nonnegative Numbers) in C++
- Check a Subarray is Formed by Consecutive Integers from a Given Array in Java
- Check if subarray with given product exists in an array in Python
- Maximum subarray sum in array formed by repeating the given array k times in C++
- Find subarray with given sum - (Handles Negative Numbers) in C++
- Maximum Subarray Sum in a given Range in C++
- Find Mean and Median of an unsorted Array in Java
- C++ program to find perfect array of size n whose subarray is a good array
- Finding degree of subarray in an array JavaScript
- Number of elements less than or equal to a given number in a given subarray in C++
