Statistics from a Large Sample - Problem

You are given a large sample of integers in the range [0, 255]. Since the sample is so large, it is represented by an array count where count[k] is the number of times that k appears in the sample.

Calculate the following statistics:

  • minimum: The minimum element in the sample.
  • maximum: The maximum element in the sample.
  • mean: The average of the sample, calculated as the total sum of all elements divided by the total number of elements.
  • median: If the sample has an odd number of elements, then the median is the middle element once the sample is sorted. If the sample has an even number of elements, then the median is the average of the two middle elements once the sample is sorted.
  • mode: The number that appears the most in the sample. It is guaranteed to be unique.

Return the statistics of the sample as an array of floating-point numbers [minimum, maximum, mean, median, mode].

Answers within 10-5 of the actual answer will be accepted.

Input & Output

Example 1 — Basic Case
$ Input: count = [0,1,3,4]
Output: [1.0,3.0,2.25,2.5,3.0]
💡 Note: Sample represents: value 1 appears 1 time, value 2 appears 3 times, value 3 appears 4 times. Min=1, Max=3, Mean=(1×1+2×3+3×4)/8=18/8=2.25, Median of [1,2,2,2,3,3,3,3] is (2+3)/2=2.5, Mode=3 (appears 4 times)
Example 2 — Single Value
$ Input: count = [0,4,0,0,0,0]
Output: [1.0,1.0,1.0,1.0,1.0]
💡 Note: Only value 1 appears 4 times. All statistics equal 1.0: min=1, max=1, mean=1, median=1, mode=1
Example 3 — Wider Range
$ Input: count = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Output: [100.0,140.0,123.33333333333333,110.0,140.0]
💡 Note: Values 100 (freq 1), 110 (freq 2), 140 (freq 3). Sample: [100,110,110,140,140,140]. Min=100, Max=140, Mean=(100+110+110+140+140+140)/6=740/6≈123.33, Median=(110+140)/2=125, Mode=140

Constraints

  • count.length == 256
  • 0 ≤ count[i] ≤ 109
  • 1 ≤ sum(count) ≤ 109
  • The mode is guaranteed to be unique

Visualization

Tap to expand
Statistics from a Large Sample: Frequency → StatisticsInput: count = [0,1,3,4] represents sample [1,2,2,2,3,3,3,3]0134val 0: 0×val 1: 1×val 2: 3×val 3: 4×Calculate Statistics Directly from FrequenciesMin: 1Max: 3Mean: 2.25Mode: 3first non-zerolast non-zeroweighted summax frequencyMedian: 2.5 (cumulative count)Output: [1.0, 3.0, 2.25, 2.5, 3.0]
Understanding the Visualization
1
Input
Frequency array where count[i] = occurrences of value i
2
Process
Calculate statistics without reconstructing full array
3
Output
Return [min, max, mean, median, mode] as floats
Key Takeaway
🎯 Key Insight: Process frequency counts directly instead of reconstructing the massive original array - achieves O(1) space complexity!
Asked in
Google 35 Amazon 28 Microsoft 22 Facebook 18
28.0K Views
Medium Frequency
~25 min Avg. Time
1.1K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen