Algorithm to determine if array contains n…n+m?

前端 未结 30 3093
清酒与你
清酒与你 2020-11-28 01:45

I saw this question on Reddit, and there were no positive solutions presented, and I thought it would be a perfect question to ask here. This was in a thread about interview

30条回答
  •  -上瘾入骨i
    2020-11-28 01:57

    ciphwn has it right. It is all to do with statistics. What the question is asking is, in statistical terms, is whether or not the sequence of numbers form a discrete uniform distribution. A discrete uniform distribution is where all values of a finite set of possible values are equally probable. Fortunately there are some useful formulas to determine if a discrete set is uniform. Firstly, to determine the mean of the set (a..b) is (a+b)/2 and the variance is (n.n-1)/12. Next, determine the variance of the given set:

    variance = sum [i=1..n] (f(i)-mean).(f(i)-mean)/n
    

    and then compare with the expected variance. This will require two passes over the data, once to determine the mean and again to calculate the variance.

    References:

    • uniform discrete distribution
    • variance

提交回复
热议问题