Let, I have an array of size N and elements of array are denoted by Array[i] where i is index here,
Now I need to find out whether any element in the given array occurs a specific amount of time or not?
Conditions are given as follows :
**Array[i] is much greater than N** for all i belongs to N
(you can think as element of array is thousand times or even more greater than the size of the arraay)
**Time Complexity = O(N) Space Complexity = O(1)**
Example=
ARRAY[] = {1111, 2222, 3333, 2222, 3333, 3333, 3333, 1111, 2222, 3333}
now is it possible to find out if any element occurs more than 4 times in given array by traversing the given array once / twice / thrice / ....
This requires destroying the array as we analyze it.
We will evaluate
n/7of the values in a single pass. We will do it by using the first6n/7of the array as a Cuckoo hash as follows.(i,j)hasj <= nthenjis not a value and that is an empty bucket.(i,j)hasjnot in one of the 2 locations where it would go in the hash, that's an empty bucket.n < i, thenjappears in the hash once.n <= i, thenjappears in the hashitimes.Note that the first
6n/7of the array will be treated as3n/7buckets, which is 3x then/7values we are trying to hash. Therefore our load factor is1/3 < 1/2and Cuckoo hashing works.I'll just outline it from here. But this is the idea.
We'll need a counter
inserted_valuesfor how many distinct values are in the hash.We initialize
inserted_valuesby scanning the hash buckets to see how many happen to have a value in a correct place to be a full bucket. Note, if a value appears in both places it could be, only the one for the first hash value is correct.And now we start from the end of the array and move towards the front. As we look at each value, we go as follows.
(NOTE: I left out rehashing. That adds considerable complications, but the principle is the same.)
And now we can scan the hash buckets, to find
n/7of the values with their counts. If any has a count meeting your condition, you have the answer. Otherwise 0 out the counts and processed values.Each stage processes
n/7of the hash, so at most 7 are needed. Each requires 2 passes, so total of 14 passes. This isO(n)of work (I left some complications around rehashing out, so let's just say with very high likelihoodO(n)work) to produce your answer.It is using the array itself to track everything. In addition you need
O(1)memory, so it meets the stated requirements.