How to tell if an array is a permutation in O(n)?

后端 未结 16 1685
粉色の甜心
粉色の甜心 2020-12-07 10:17

Input: A read-only array of N elements containing integer values from 1 to N (some integer values can appear more than once!). And a memory zone of a fixed<

16条回答
  •  不知归路
    2020-12-07 10:46

    This isn't going to work due to the complexity being given as a function of N rather than M, implying that N >> M

    This was my shot at it, but for a bloom filter to be useful, you need a big M, at which point you may as well use simple bit toggling for something like integers

    http://en.wikipedia.org/wiki/Bloom_filter

    For each element in the array Run the k hash functions Check for inclusion in the bloom filter If it is there, there is a probability you've seen the element before If it isn't, add it

    When you are done, you may as well compare it to the results of a 1..N array in order, as that'll only cost you another N.

    Now if I haven't put enough caveats in. It isn't 100%, or even close since you specified complexity in N, which implies that N >> M, so fundamentally it won't work as you have specified it.

    BTW, the false positive rate for an individual item should be e = 2^(-m/(n*sqrt(2)))

    Which monkeying around with will give you an idea how big M would need to be to be acceptable.

提交回复
热议问题