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:
- Every element will be between 0 and K (inclusive) by the pigeonhole principle.
- 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).
- 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.
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 thanK? Well if that is true, then all the numbers{0,1,...K}are present in the above set and that isK+1numbers should be present in the above set, which consists ofKnumbers. This is not possible and hense a contradiction with the assumption that the new number is more thanK. So on each step the next number you add will be in the range[0,K]and thus on theK+1step all the lastK+1numbers will be in that step and thus be different numbers in that range.