Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the min Facebook Hacker cup 2013 [closed]

Tags:

algorithm

Since the contest is now over I would like to ask about this question. I've been really struggling with this question for the past three days because actually I'm not able to understand it.

Here's the question: Find the min Facebook Hacker Cup 2013

Here's the site where I found a solution for this question: Solutions

What I'm not able to understand is the following part:

Although these first K values could be anything, we can make some useful observations about the contents of the array after the initial K elements:

  1. Every element will be between 0 and K (inclusive) by the pigeonhole principle.
  2. Consequently, every window of K + 1 consecutive elements will contain each value between 0 and K exactly once (i.e. it contains a permutation of the integers 0 through K).
  3. Consequently, for i > 2K: M[i] = M[i - (K + 1)].

Though I'm able to understand the second and third points iff the first is true. The first one is actually troubling me. Why do the elements have to be between 0 and K why can't all of them just be the minimum non-negative integer which is not present in the initial K elements.

For eg: For the following test case:

1

46 18

7 11 9 46

The first K elements are:

[7, 40, 35, 26, 19, 34, 15, 36, 37, 2, 31, 28, 41, 0, 9, 16, 1, 20]

Now why do the rest of the elements have to be between 0 and 18, when I have elements which are more than 18 in my previous K elements.

like image 392
Kartik Anand Avatar asked Nov 21 '25 19:11

Kartik Anand


1 Answers

Imagine you have K numbers {a0,a1,a2...a(k-1)}, now you want to find the first non-negative number that is not among them. Can it be more than K? Well if that is true, then all the numbers {0,1,...K} are present in the above set and that is K+1 numbers should be present in the above set, which consists of K numbers. This is not possible and hense a contradiction with the assumption that the new number is more than K. So on each step the next number you add will be in the range [0,K] and thus on the K+1 step all the last K+1 numbers will be in that step and thus be different numbers in that range.

like image 101
Ivaylo Strandjev Avatar answered Nov 24 '25 17:11

Ivaylo Strandjev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!