2985. Calculate Compressed Mean
Calculate Compressed Mean (LeetCode 2985) is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Sliding Window pattern, in the Fixed Size (Subarray Calculation) subpattern.
How to solve Calculate Compressed Mean
Calculate Compressed Mean is a Fixed Size Sliding Window problem, part of Sliding Window. Learn fixed-size sliding window for subarray calculations like maximum average, moving average, and k-size subarray problems.
Work towards the solution in these steps:
- The mean is the sum of the quantities divided by how many there are.
- One pass is enough: keep a running total; the count is the array's length.
- Handle the empty array before dividing, and round only the final result to 2 decimal places.
The reference solution runs in O(N) time and O(1) space.
The Calculate Compressed Mean solution and editorial walks through the full approach with reference code.
Problem statement
You are given an integer array quantities, where quantities[i] is the number of items in the i-th order.
Return the mean (average) number of items per order, rounded to 2 decimal places. If quantities is empty, return 0.0.
This is the array form of LeetCode 2985: there the orders arrive compressed as (itemcount, orderoccurrences) rows; here every order is listed individually.
Examples
Example 1
Input: {"quantities": [10, 20, 30, 40]}
Output: 25
Explanation: The mean of [10, 20, 30, 40] is (10+20+30+40)/4 = 25.
Example 2
Input: {"quantities": [5, 5, 5, 5, 5]}
Output: 5
Explanation: All values are the same, so the mean is 5.
Topics and companies
Topics: Array, Math.
Reported in interviews at Google, Amazon, Facebook, Microsoft.
How to practise Calculate Compressed Mean on Thita.ai
Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(N) time and O(1) space.
Open Calculate Compressed Mean in the code editor, or read the Calculate Compressed Mean editorial for a worked solution with its approach and complexity analysis.
Where Calculate Compressed Mean sits in the DSA pattern sheet
Problems related to Calculate Compressed Mean
Other problems that use the same Fixed Size (Subarray Calculation) and Sliding Window techniques:
- Find the Power of K-Size Subarrays I — Medium, same subpattern
- Find X-Sum of All K-Long Subarrays I — Easy, same subpattern
- Maximum Average Subarray I — Easy, same subpattern
- Moving Average from Data Stream — Easy, same subpattern
- Contains Duplicate II — Easy, same pattern
- Continuous Subarrays — Medium, same pattern
Calculate Compressed Mean FAQ
What is the LeetCode number of Calculate Compressed Mean?
Calculate Compressed Mean is LeetCode problem 2985, rated Easy.
What pattern is used to solve Calculate Compressed Mean?
Calculate Compressed Mean is a Fixed Size Sliding Window problem. It belongs to the Sliding Window pattern, in the Fixed Size (Subarray Calculation) subpattern.
What is the time complexity of Calculate Compressed Mean?
The reference solution runs in O(N) time and O(1) space.
Which companies ask Calculate Compressed Mean?
Calculate Compressed Mean has been reported in interviews at Google, Amazon, Facebook, Microsoft.