Solution:

There are two things to keep in mind:

Firstly there is at least one red hat. (There can be two or three as well).

Secondly the competition is fair for everyone.



Thus if there is only one red hat, that person will see two green hats on other heads and will be able to deduce his own color as red. However the other students will see one red and one green hat and can never be sure. In such manner, the competition will prove to be partial for one student.



Suppose if there are two red hats. Then the students who are wearing red hats will see one red and one green hat on others. Now they must have deduced that there can’t be just one red hat. Thus they will know that they are also wearing a red hat. But the one who is wearing a green hat will see two red hats and can never be sure of his own color. In this case as well, the competition will not be fair.



Thus the only possible and fair means is if all of them are wearing a red hat. The one who is able to deduce the situation first, will raise his hand and will tell the correct answer.