Consider we have a sequence of numbers arriving in sequential order (N numbers in total). How to develop a one-pass (that is, during the sequence arrival) O(N) algorithm to find the number (and it’s position in the sequence) of minimal nonzero magnitude? Note that standard simple algorithm doesn’t work here, since the initial number could be zero.
Consider we have a sequence of numbers arriving in sequential order (N numbers in
Share
You need to consider the possible cases involved in processing each number in the sequence, ie: is it zero or non-zero and if non-zero is it the first non-zero one or not? Then have the alogrithm deal with each case. I’d recommend using a logical flag to track the latter case.