Simple Moving Average is the average obtained from the data for some t period of time . In normal mean, its value get changed with the changing data but in this type of mean it also changes with the time interval. We get the mean for some period t and then we remove some previous data. Again we get new mean and this process continues. This is why it is moving average. This has a great application in the financial market. OR this can be simply visualized as follows.
Given an arr[] of size N, containing only positive integers and an integer K. The task is to compute the simple moving average of previous K elements.
Examples:
Input: { 1, 3, 5, 6, 8 }, K = 3
Output: 0.33 1.33 3.00 4.67 6.33
Explanation: New number added is 1.0, SMA = 0.33
New number added is 3.0, SMA = 1.33
New number added is 5.0, SMA = 3.0
New number added is 6.0, SMA = 4.67
New number added is 8.0, SMA = 6.33
Input: Array[]= {2, 5, 7, 3, 11, 9, 13, 12}, K = 2
Output: 1.0 3.5 6 5 7 10 11 12.5
Naive Approach: This uses two nested loops. Outer loop traverses the array from left to right. Inner loop computes the average of K previous elements including itself for each index. Finally, the moving average values are printed. The outer loop starts traversal from index K itself. Instead of storing the result, we can directly display the output to avoid using extra spaces.
Time complexity: O(N*K)
Space complexity: O(1)
Efficient Approach: The efficient approach is discussed in the Set-1 of this problem.
Space Optimized approach: This uses sliding window for better time efficiency and space optimization. A window of size K starts from index K and the moving average is printed for each index thereafter.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
void ComputeMovingAverage( int arr[], int N,
int K)
{
int i;
float sum = 0;
for (i = 0; i < K; i++) {
sum += arr[i];
cout << setprecision(2) << std::fixed;
cout << sum / K << " " ;
}
float avg;
for (i = K; i < N; i++) {
sum -= arr[i - K];
sum += arr[i];
avg = sum / K;
cout << setprecision(2) << std::fixed;
cout << avg << " " ;
}
}
int main()
{
int arr[] = { 1, 3, 5, 6, 8 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 3;
ComputeMovingAverage(arr, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void ComputeMovingAverage( int arr[], int N,
int K)
{
int i;
float sum = 0 ;
for (i = 0 ; i < K; i++) {
sum += arr[i];
System.out.printf( "%.2f " ,sum / K);
}
for (i = K; i < N; i++) {
sum -= arr[i - K];
sum += arr[i];
System.out.printf( "%.2f " ,sum / K);
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 3 , 5 , 6 , 8 };
int N = arr.length;
int K = 3 ;
ComputeMovingAverage(arr, N, K);
}
}
|
Python3
def ComputeMovingAverage(arr, N, K):
i = None
sum = 0
for i in range (K):
sum + = arr[i]
print ( "%.2f" % ( sum / K), end = " " )
for i in range (K, N):
sum - = arr[i - K]
sum + = arr[i]
avg = sum / K
print ( "%.2f" % (avg), end = " " )
arr = [ 1 , 3 , 5 , 6 , 8 ]
N = len (arr)
K = 3
ComputeMovingAverage(arr, N, K)
|
C#
using System;
class GFG {
static void ComputeMovingAverage( int [] arr, int N,
int K)
{
int i;
float sum = 0;
for (i = 0; i < K; i++) {
sum += arr[i];
Console.Write(Math.Round((sum / K),2) + " " );
}
for (i = K; i < N; i++) {
sum -= arr[i - K];
sum += arr[i];
Console.Write(Math.Round(sum / K, 2) + " " );
}
}
public static void Main( string [] args)
{
int [] arr = { 1, 3, 5, 6, 8 };
int N = arr.Length;
int K = 3;
ComputeMovingAverage(arr, N, K);
}
}
|
Javascript
<script>
function ComputeMovingAverage(arr, N,
K) {
let i;
let sum = 0;
for (i = 0; i < K; i++) {
sum += arr[i];
document.write((sum / K).toFixed(2) + " " );
}
for (i = K; i < N; i++) {
sum -= arr[i - K];
sum += arr[i];
avg = sum / K;
document.write((avg).toFixed(2) + " " );
}
}
let arr = [1, 3, 5, 6, 8];
let N = arr.length;
let K = 3;
ComputeMovingAverage(arr, N, K);
</script>
|
Output
0.33 1.33 3.00 4.67 6.33
Time complexity: O(N)
Space complexity: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!