A group of people numbered are going to your house for a party. To not let anyone feels left out, you want to befriend the one with the least number of friends. To achieve that, you will ask each of them the question "How many friends do you have?" and note down their response. Before working out the details, you want to make sure that no one give false information. That is, there is at least one possible network of friendships that matches the responses.
Below is a list of responses taken from a group of 10 people. Answer
True
if it's possible to find a network of friendships that matches,
False
otherwise.
7 3 1 8 3 6 5 1 8 4
Clarifications :
Friendship is mutual. If is 's friend, then is 's friend as well.
You are not counted as their friends at the moment.
You are encouraged to solve the Easy version of this problem first.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
The list after we sort by decreasing number of friends is
We would try to build a network of friendship based on the list above. Now, we only know the number of friends each person has, but the matching is all up to us. If one has x friends, then we must be able to find x people that has at least one available slot for a friendship, and reduce the available number of friendship by 1 . If every number in the list can be reduced to 0 , then the network exist.
The question now is how should we match . First, we consider the person who currently has the most number of friends (say x friends), then we identify the remaining x people who has the most number of friends and reduce them by 1 . If we couldn't find x available people, we conclude that we couldn't form the network. Look at how this algorithm affects the list :
Now, only the 5 -th and 6 -th person has remaining slots and others have fully matched. We couldn't find 3 person to match with the 4 -th person. Therefore, the network cannot be constructed.