Maximise the happiness among children Given N different candies and  M childrens. Each ith children demands for K[i] different candies and will ONLY BE HAPPY IFF HE GET ALL THOSE DIFFERENT CANDIES WHICH HE DEMANDED.

Now i want that maximum children should be happy.So how should i distribute the candies to do the same?

I need to find maximum children that i can make happy.

EXAMPLE : 

Let we have N=4 candies and M=3 children
Now 

1st children require 2 (=K[1]) candies which are : 1 and 2 

2nd children require 2 (=K[2]) candies which are : 2 and 3 

3rd children require 2 (=K[3]) candies which are : 3 and 4 

Then here answer will be 2 as i can at max only make 1st and 3rd child happy.
Construct a graph as follows:


Each vertex represents a child
Two vertexes share an edge iff the children are exclusive in terms of happiness; that is, if there exists a piece of candy that both children want then they share an edge


Now your problem is to find the maximum number of vertices which do not share an edge.  This is called an "independent set".  Unfortunately, this is NP-Hard.  Solving it exactly is not known in polynomial time, but it is a well studied problem so you have options.

The problem of finding the maximum independent set is the same as the problem as finding the maximum clique in the dual graph, so you may see the problem described that way.

http://en.wikipedia.org/wiki/Maximum_independent_set#Finding_maximum_independent_sets





This is the example from your question.  The 3 vertexes are the circles which represent the 3 children.  The red edge represents that the first and second child both want candy number 2.  The blue edge represents that the second and third child both want candy 3.

The maximum independent set is 1st and 3rd since they do not share an edge.
This is a greedy algorithm: you have give the candy that they demand in the order of the amounts that they require to be happy (i.e. K[i]). In other words, you should only give candy to a child if you have made happy all the children that demand less, and every time you give candy to one of them you have to give them the whole amount that they require.