Skip to content

Instantly share code, notes, and snippets.

@progheal
Last active June 18, 2016 13:14
Show Gist options
  • Save progheal/9b0b1fc4651c6238e52efbd04fbe20fd to your computer and use it in GitHub Desktop.
Save progheal/9b0b1fc4651c6238e52efbd04fbe20fd to your computer and use it in GitHub Desktop.
Proper solution of the challenge `KingChicken`: https://codefights.com/challenge/3vZaAk4SrnfoHgr9C

For odd number of students, it's easy to construct a situation that all of them are king chicken:
Label them from 1 to n, every one pecks the (n-1)/2 students numbered after him (labels wrap around from n to 1).
In this case, for every people, those (n-1)/2 students pecked him is pecked by the last people he pecked.
For example, for 7 students (Test 4),
Student 1 pecks Student 2,3,4, and Student 4 pecks Student 5,6,7, make Student 1 king chicken;
Student 2 pecks Student 3,4,5, and Student 5 pecks Student 6,7,1, make Student 2 king chicken;
etc.

For even number of students, things get interesting:
n=2 is impossible for both of them to be king; one must peck the other.
n=4 requres a little bit of argument:
There sure exists a student A that pecks two student B and C, but pecked by the last student D.
(If there exists a student pecked all three, only he is king as no one pecked him; if none of the students pecked two or more students, for any of them he and his sole servant can only cover at most 2 students)
B and C must peck D if they want to be king as D is the only one pecked A. This leaves the pecking between B and C. It is easy to see that if B pecks C, C cannot be king, but all other three students are kings. The case of C pecks B is similar.

For n>=6, We can construct a situation that all of them are king chickens:
All odd numbered student pecks the n/2 students numbered after him (numbers also wrap around), and even numbered students peck the student at two position before him and all odd numbered students that doesn't peck him.
Below is the detail of this arrangement, for n=6, 8, 10, and 12:

n = 6       n = 8         n = 10           n = 12
1->2,3,4    1->2,3,4,5    1->2,3,4,5,6     1->2,3,4,5,6,7
2->6,3      2->8,3,5      2->10,3,5        2->12,3,5,7
3->4,5,6    3->4,5,6,7    3->4,5,6,7,8     3->4,5,6,7,8,9
4->2,5      4->2,5,7      4->2,5,7         4->2,5,7,9
5->6,1,2    5->6,7,8,1    5->6,7,8,9,10    5->6,7,8,9,10,11
6->4,1      6->4,7,1      6->4,7,9         6->4,7,9,11
            7->8,1,2,3    7->8,9,10,1,2    7->8,9,10,11,12,1
            8->6,1,3      8->6,9,1         8->6,9,11,1
                          9->10,1,2,3,4    9->10,11,12,1,2,3
                          10->8,1,3        10->8,11,1,3
                                           11->12,1,2,3,4,5
                                           12->10,1,3,5

The validity of these arrangement can be proved separately for 4k and 4k+2; it can be summarized by studying the configuration above. And actually, for n=4k we can eliminate the pecking loop between even numbered students; they are there for the 4k+2 case to make the student one position before him to be pecked by this servant at two position before him.

The solution I pasted into comment:

A pecks B,D,E
B pecks C,E,F
C pecks A,D,F
D pecks B,F
E pecks C,D
F pecks A,E

is simply a substitution of the above arrangement for n=6: 1->A, 3->B, 5->C; 2->D, 4->E, 6->F. The reason to rearrange them is that I don't want this pattern too obvious, give other people time to really think about it.

A side note: It is easy to construct case for even n that have n-1 students be king; just use a configuration for odd n, and add one last poor student pecked by all others. I think this is why it's easy to guess that even n can "only" achieve n-1 kings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment