Given a stream of integers (I can only go through them once), what is the best solution to find maximum and minimum? I suppose that in case I have enough time to proccess each number the easiest solution is to simply keep min and max values in a separate variables, but what is the best approach if I'm unable to proccess every single one of them? Is there some better solution than simply keep max and min variables and skip for example every second number?
Asked
Active
Viewed 1,540 times
0
-
2What do you mean by "unable to process every single one of them"? What would you do if you skipped some, and the maximum was one of the ones you skipped? – Greg Hewgill Dec 03 '12 at 07:34
-
5If you had a list of numbers on a piece of paper, how would you find the max and min without checking each one?? What if you got unlucky and you skipped over the maximum - you wouldn't know without looking at the number at least once, right? – lc. Dec 03 '12 at 07:34
-
If you can't iterate over all the numbers, then how will you know that the ones you obtain (`max` and `min`) are truly the largest and smallest numbers? – npinti Dec 03 '12 at 07:35
-
If you are saving the integers locally in an array and can keep the array sorted, the min and max will always be the first and last number. – JensB Dec 03 '12 at 07:36
-
2If you want the `min` and `max` exactly, then you need to go through all of them. If you want to approximate `min` and `max`, you can try something called Newton-Raphson Method – andreih Dec 03 '12 at 07:39
-
1@JensBerfenfeldt keeping the data in a sorted array clearly involves looking at all elements and is not suited for stream processing. – Has QUIT--Anony-Mousse Dec 03 '12 at 07:44
-
Of course if I wasn't able to go through all the numbers, I could accidentaly miss the actual min or max. I was thinking about some way of guessing/approximation from the numbers I have. The Newton-Raphson method looks best for this so far. – Mates Dec 03 '12 at 10:06
1 Answers
0
If you want the true maximum and minimum, just track them with variables.
Depending on your input data, a probabilistic min/max may be "accurate enough". So if you only look at every number with 50% chance, then you probably have only a 50% of having the exact min or max. But maybe already a 75% chance of having at least the second largest/smallest etc. However, computing the random number to do an unbiased sampling is already more expensive than looking at all numbers for min/max. Skipping every second is dangerous: there might be an even/odd pattern in the data that screws you badly.

Has QUIT--Anony-Mousse
- 76,138
- 12
- 138
- 194