Блог пользователя prac64

Автор prac64, история, 9 лет назад, По-английски

Hey CodeForces, recently I came accross a problem, finding an element in an array which occurs more than n/3 times, while using constant space and linear time. Now I admit, this is a well known interview problem and plenty of articles exist which cover this, however I have struggled to find one that offers proof of correctness or any explanation.

Please help me understand the correctness.

Article: https://www.geeksforgeeks.org/given-an-array-of-of-size-n-finds-all-the-elements-that-appear-more-than-nk-times/

Basic Idea: Like we do in Boyer-Moore algorithm, however instead of keeping one element and counter, keep 2 , then update them similar to the original algorithm.

Thank you CodeForces!

  • Проголосовать: нравится
  • +4
  • Проголосовать: не нравится

»
9 лет назад, скрыть # |
← Rev. 3  
Проголосовать: нравится +24 Проголосовать: не нравится

You can have a randomized algorithm. That is -
Take a random number from the array, and check if it appears more than times. If you repeat this x times, then you have probability of hitting a number with frequency more than .
If n = 105 and k = 3, then if you check only 40 random numbers, you have 99% probability of hitting such a number.
It may have complexity O(nx) or O(n + x) depending on the bounds of the numbers of array, and linear space. However, this may not be fesible always if x needs to be high.

This idea can be used to solve this problem — 840D - Destiny

»
9 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Problem: Find all elements of A, |A| = n, that appear more than times.

Let's split A in two smaller subproblems A1, |A1| = n1, and A2, |A2| = n2, such that n = n1 + n2. We can define Ki = (a1, a2, ..., ak) as the k greatest elements by frequency in subproblem Ai and F(x, Ai) as the frequency of element x in Ai.

Then we argue that the elements of K1 and K2 are the only candidates to appear more than times in the original problem (K).

Let's do a proof by contradiction and assume that there is an element x such that , and . We know that . Using the converse we know that and then:

F(x, A) = F(x, A1) + F(x, A2)

Contradiction!!

So, the elements of K1 and K2 are the only candidates to appear more than times in the original problem (K).

For a more formal proof, use strong induction on |A1| + |A2| within the induction on |A|. Regards.

»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Nevermind thought out a rough proof:

Consider k-1 stacks(for now, we will reduce to counters later) and the remaining array that is to be seen, now instead of incrementing the counter, imagine you push the same value of the element on the respective stack. similarly for decrementing, imagine you remove one element from every stack.

Now define solution set as elements in stack+ remaining array. we can remove elements from solution set only when it contains k distinct elements.. that is k-1 distinct elements in the stacks and a new element from the remaining array. so now we discard k distinct elements from the solution set.

After we are finished with the entire array, we would have discarded only tuples of k distinct elements. It is now very clear that elements with frequency greater(strictly) than n/k remain in the stack. If the array did not contain any greater-than-n/k-elements, the stack will have garbage values. Hence we need to check once more in (k-1)n time.

Instead of having stacks with same elements he compressed it into value+num_instances pairs.