TensorTonicTensorTonic
Problems
Study PlansProjectsInterviewPricingFeedback
Problems
Loading...
1 / 1

Simple Moving Average

Time Series
Easy

The Simple Moving Average (SMA) is the most basic time series smoothing technique. It computes the unweighted mean of a sliding window of consecutive observations, producing a smoother signal that filters out short-term fluctuations and highlights longer-term trends.

Given a list of numeric values and a window size k, compute the SMA for each valid window position.

Algorithm

For each position i from 0 to n - k, compute the average of k consecutive values:

SMA[i]=1k∑j=0k−1x[i+j]\text{SMA}[i] = \frac{1}{k} \sum_{j=0}^{k-1} x[i+j]SMA[i]=k1​j=0∑k−1​x[i+j]

The output has length n - k + 1, where n is the input length.

Loading visualization...

Examples

Input:

values = [1, 2, 3, 4, 5], window_size = 3

Output:

[2.0, 3.0, 4.0]

Three windows: (1+2+3)/3=2.0, (2+3+4)/3=3.0, (3+4+5)/3=4.0.

Input:

values = [10, 20, 30, 40], window_size = 2

Output:

[15.0, 25.0, 35.0]

Three windows of size 2: (10+20)/2=15.0, (20+30)/2=25.0, (30+40)/2=35.0.

Hint 1

Loop from i = 0 to len(values) - window_size. At each position, take the slice values[i:i+window_size], sum it, and divide by window_size.

Hint 2

For efficiency, you can maintain a running sum: subtract the element leaving the window and add the element entering. But the straightforward approach of summing each window works within the time limit.

Requirements

  • Compute the arithmetic mean of each sliding window of size k
  • The output length should be n - window_size + 1
  • Return a list of floats representing the moving averages

Constraints

  • values has at least 1 element
  • 1 <= window_size <= len(values)
  • Return a list of floats
  • Time limit: 300 ms
Try Similar Problems
Weighted Moving AverageExponential Moving AverageMoving MedianLag FeaturesRolling Standard Deviation

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

You must run your code first.
PrevNext

Simple Moving Average

Time Series
Easy

The Simple Moving Average (SMA) is the most basic time series smoothing technique. It computes the unweighted mean of a sliding window of consecutive observations, producing a smoother signal that filters out short-term fluctuations and highlights longer-term trends.

Given a list of numeric values and a window size k, compute the SMA for each valid window position.

Algorithm

For each position i from 0 to n - k, compute the average of k consecutive values:

SMA[i]=1k∑j=0k−1x[i+j]\text{SMA}[i] = \frac{1}{k} \sum_{j=0}^{k-1} x[i+j]SMA[i]=k1​j=0∑k−1​x[i+j]

The output has length n - k + 1, where n is the input length.

Loading visualization...

Examples

Input:

values = [1, 2, 3, 4, 5], window_size = 3

Output:

[2.0, 3.0, 4.0]

Three windows: (1+2+3)/3=2.0, (2+3+4)/3=3.0, (3+4+5)/3=4.0.

Input:

values = [10, 20, 30, 40], window_size = 2

Output:

[15.0, 25.0, 35.0]

Three windows of size 2: (10+20)/2=15.0, (20+30)/2=25.0, (30+40)/2=35.0.

Hint 1

Loop from i = 0 to len(values) - window_size. At each position, take the slice values[i:i+window_size], sum it, and divide by window_size.

Hint 2

For efficiency, you can maintain a running sum: subtract the element leaving the window and add the element entering. But the straightforward approach of summing each window works within the time limit.

Requirements

  • Compute the arithmetic mean of each sliding window of size k
  • The output length should be n - window_size + 1
  • Return a list of floats representing the moving averages

Constraints

  • values has at least 1 element
  • 1 <= window_size <= len(values)
  • Return a list of floats
  • Time limit: 300 ms
Try Similar Problems
Weighted Moving AverageExponential Moving AverageMoving MedianLag FeaturesRolling Standard Deviation

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

You must run your code first.