Fast calculation of min, max, and average of incoming numbers
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
The ability to efficiently calculate the minimum, maximum, and average of a collection of incoming numbers in real-time is highly valuable in computer science, particularly in data analytics, digital signal processing, and various applications which involve massive datasets. This article delves into strategies and algorithms that enable these computations to be performed quickly and accurately, even as new numbers continuously arrive.
Introduction
Continuous data streams are common in various applications such as financial tick updates, sensor data monitoring, and video game rendering. When managing these streams, it's essential to perform calculations with minimal latency and memory footprint. Traditional batch processing methods can be inefficient as they might require full dataset reevaluation with each incoming number. Instead, more sophisticated techniques can leverage ongoing calculations to update results incrementally.
Basic Concepts
Min and Max Calculation
When a new number arrives in a stream, determining the new minimum or maximum requires a comparison between the new number and the current min/max. This operation is (constant time complexity), making it efficient:
• Min Calculation: `current_min = min(current_min, new_number)` • Max Calculation: `current_max = max(current_max, new_number)`
Average Calculation
For continuous average updates, it's crucial to store the cumulative sum of all numbers encountered and the count of numbers processed. The average is calculated using:
This approach ensures a constant time complexity for updating the average.
Advanced Techniques
Sliding Window Approach
In real-time systems where only a fixed number of the most recent data points are relevant, a sliding window can optimize calculations:
• Data Structures: Use a double-ended queue (deque) for maintaining candidates for min/max within the window. • Algorithm: As the window moves, remove elements that are out of scope while ensuring that the deque's min/max properties hold.
Exponential Moving Average (EMA)
The EMA is particularly useful for smooth averaging of fluctuating data:
Here, is a smoothing factor between 0 and 1. This approach gives more weight to recent observations, making it sensitive to the latest data.
Implementations and Considerations
Here is an example implementation of maintaining real-time min, max, and average in Python:
Related reading
- Fast CRC algorithm?
- Fast Data structure for finding strict subsets from a given list
- Fast division algorithm for binary numbers
- fast efficient least squares fit algorithm in C?
- Fast integer matrix multiplication with bit-twiddling hacks
- Fast Method for computing 3x3 symmetric matrix spectral decomposition
- Fast element lookup for a functional languageHaskell
- Fast Fibonacci recursion

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.