# Program to find simple moving average | Set-2

**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 = 3Output:0.33 1.33 3.00 4.67 6.33Explanation: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 = 2Output: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)

*O(1)*

**Space complexity:****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++

`// C++ code to find the simple moving average` `#include <bits/stdc++.h>` `#include <iomanip>` `using` `namespace` `std;` `// Function to compute moving average` `// of previous K elements` `void` `ComputeMovingAverage(` `int` `arr[], ` `int` `N,` ` ` `int` `K)` `{` ` ` `int` `i;` ` ` `float` `sum = 0;` ` ` `// Initial sum of K elements.` ` ` `for` `(i = 0; i < K; i++) {` ` ` `sum += arr[i];` ` ` `cout << setprecision(2) << std::fixed;` ` ` `cout << sum / K << ` `" "` `;` ` ` `}` ` ` `// Compute MA from index 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 << ` `" "` `;` ` ` `}` `}` `// Driver code` `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

`// Java code to find the simple moving average` `import` `java.util.*;` `class` `GFG{` ` ` `// Function to compute moving average` ` ` `// of previous K elements` ` ` `static` `void` `ComputeMovingAverage(` `int` `arr[], ` `int` `N,` ` ` `int` `K)` ` ` `{` ` ` `int` `i;` ` ` `float` `sum = ` `0` `;` ` ` `// Initial sum of K elements.` ` ` `for` `(i = ` `0` `; i < K; i++) {` ` ` `sum += arr[i];` ` ` `System.out.printf(` `"%.2f "` `,sum / K);` ` ` `}` ` ` `// Compute MA from index K` ` ` `for` `(i = K; i < N; i++) {` ` ` `sum -= arr[i - K];` ` ` `sum += arr[i];` ` ` `System.out.printf(` `"%.2f "` `,sum / K);` ` ` `}` ` ` `}` ` ` `// Driver code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `arr[] = { ` `1` `, ` `3` `, ` `5` `, ` `6` `, ` `8` `};` ` ` `int` `N = arr.length;` ` ` `int` `K = ` `3` `;` ` ` `ComputeMovingAverage(arr, N, K);` ` ` `}` `}` `// This code is contributed by Rajput-Ji` |

## Python3

`# Python code for the above approach` `# Function to compute moving average` `# of previous K elements` `def` `ComputeMovingAverage(arr, N, K):` ` ` `i ` `=` `None` ` ` `sum` `=` `0` ` ` `# Initial sum of K elements.` ` ` `for` `i ` `in` `range` `(K):` ` ` `sum` `+` `=` `arr[i]` ` ` `print` `(` `"%.2f"` `%` `(` `sum` `/` `K), end` `=` `" "` `)` ` ` `# Compute MA from index K` ` ` `for` `i ` `in` `range` `(K, N):` ` ` `sum` `-` `=` `arr[i ` `-` `K]` ` ` `sum` `+` `=` `arr[i]` ` ` `avg ` `=` `sum` `/` `K` ` ` `print` `(` `"%.2f"` `%` `(avg), end ` `=` `" "` `)` `# Driver code` `arr ` `=` `[` `1` `, ` `3` `, ` `5` `, ` `6` `, ` `8` `]` `N ` `=` `len` `(arr)` `K ` `=` `3` `ComputeMovingAverage(arr, N, K)` `# This code is contributed by Saurabh Jaiswal` |

## C#

`// C# code to find the simple moving average` `using` `System;` `class` `GFG {` ` ` `// Function to compute moving average` ` ` `// of previous K elements` ` ` `static` `void` `ComputeMovingAverage(` `int` `[] arr, ` `int` `N,` ` ` `int` `K)` ` ` `{` ` ` `int` `i;` ` ` `float` `sum = 0;` ` ` `// Initial sum of K elements.` ` ` `for` `(i = 0; i < K; i++) {` ` ` `sum += arr[i];` ` ` `Console.Write(Math.Round((sum / K),2) + ` `" "` `);` ` ` `}` ` ` `// Compute MA from index K` ` ` `for` `(i = K; i < N; i++) {` ` ` `sum -= arr[i - K];` ` ` `sum += arr[i];` ` ` `Console.Write(Math.Round(sum / K, 2) + ` `" "` `);` ` ` `}` ` ` `}` ` ` `// Driver code` ` ` `public` `static` `void` `Main(` `string` `[] args)` ` ` `{` ` ` `int` `[] arr = { 1, 3, 5, 6, 8 };` ` ` `int` `N = arr.Length;` ` ` `int` `K = 3;` ` ` `ComputeMovingAverage(arr, N, K);` ` ` `}` `}` `// This code is contributed by ukasp.` |

## Javascript

`<script>` ` ` `// JavaScript code for the above approach` ` ` `// Function to compute moving average` ` ` `// of previous K elements` ` ` `function` `ComputeMovingAverage(arr, N,` ` ` `K) {` ` ` `let i;` ` ` `let sum = 0;` ` ` `// Initial sum of K elements.` ` ` `for` `(i = 0; i < K; i++) {` ` ` `sum += arr[i];` ` ` `document.write((sum / K).toFixed(2) + ` `" "` `);` ` ` `}` ` ` `// Compute MA from index K` ` ` `for` `(i = K; i < N; i++) {` ` ` `sum -= arr[i - K];` ` ` `sum += arr[i];` ` ` `avg = sum / K;` ` ` `document.write((avg).toFixed(2) + ` `" "` `);` ` ` `}` ` ` `}` ` ` `// Driver code` ` ` `let arr = [1, 3, 5, 6, 8];` ` ` `let N = arr.length;` ` ` `let K = 3;` ` ` `ComputeMovingAverage(arr, N, K);` ` ` `// This code is contributed by Potta Lokesh` ` ` `</script>` |

**Output**

0.33 1.33 3.00 4.67 6.33

* Time complexity:* O(N)

*O(1)*

**Space complexity:**