It is an example of act utilitarianism which states that person's actions become right if it produces best possible results for the greatest number of people.
It is based on the principle of utility and according to Jeremy Bentham's well-known phrase, "the greatest happiness for the greatest number". Bentham's idea is a hedonistic one which states that people have to avoid pain in order to have the full pleasure.