# Puzzle | Message Spreading

There are n students in a class, each in possession of a different funny story. As the students were getting bored in the class, they decided to come up with a game so that they can pass their time. They want to share the funny stories with each other by sending electronic messages. Assume that a sender includes all the funny stories he or she knows at the time the message is sent and that a message may only have one addressee. What is the minimum number of messages they need to send to guarantee that everyone of them gets all the funny stories?

**Solution :**The minimum number of messages is equal to 2n – 2. There are several ways to do this.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.

**Method 1 :**The students can designate one student, say, student 1, to whom everybody else sends the message with the funny story they know. After receiving all these messages, student 1 combines all the funny stories with his or her funny story and send this combined message to each of the other n – 1 students. It can be understood by the below figure. Represent the n students as S1, S2, S3, ………….., Sn. Students designate S1 to whom every other student sends the message with the funny story they know.

After receiving all these messages, student 1 combines all the funny stories with his or her funny story and send this combined message to each of the other n – 1 students. Hence, the minimum number of messages is equal to 2n – 2.

**Method 2 (Greedy Algorithm):**Number the students from 1 to n as S1, S2, S3,……………, Sn and send the first n – 1 messages as follows : from S1 to S2, from S2 to S3, and so on, until the message combining the funny stories initially known to students S1, S2, . . . , Sn – 1 is sent to person n. Then send the message combining all the n funny stories from student n i.e, Sn to students S1, S2, . . . , Sn – 1.

Hence, The minimum number of messages is equal to 2n – 2.

**Note :**The fact that 2n – 2 messages is the smallest number needed to solve the puzzle stems from the fact that an increase of the number of students by one requires at least two extra messages i.e, to and from the extra student, exactly what the above methods provide.

Reference : Algorithmic Puzzles – Anany Levitin, Maria Levitin