Here also, there are three cases:
1.Shirish occupies the first room
2.Shirish occupies the fifth room
3.Shirish occupies a room in between
Case 1: Suppose Shirish occupies the first room. Then, Anand cannot occupy the second room.
S × _ _ _
Anand can occupy one of the remaining 3 rooms. After Anand occupies one of the three rooms, we are left with the other three boys. They are allowed to occupy the room next to Anand. For example, if Anand occupies the third room, the other three boys can occupy the second, fourth or fifth rooms.
S _ A _ _
So, the remaining 3 boys can occupy the three rooms in 3! ways. Thus, we can choose a room for Anand in three ways and, after that, we can arrange the remaining 3 boys in 3! ways. So, the total number of ways in which Shirish can occupy the first room is 3 × 3! = 18.
Case 2: Suppose Shirish occupies the last room. Again, arguing as in case 1, the number of arrangements in this case is 18.
Case 3: Suppose Shirish occupies a room in between. There are 3 possibilities in this case. If Shirish occupies the third room, Anand cannot occupy the second or fourth rooms.
_ × S × _
This shows a particular situation where Shirish occupies the third room. So, Anand can occupy any of the remaining 2 rooms. After Shirish and Anand are allotted rooms, there are 3 rooms left.
A _ S _ _
This shows a particular situation where Shirish occupies the third room and Anand occupies the first room. The remaining 3 boys can occupy any of the 3 rooms in 3! ways. So, the total number of ways in this case
3(Shirish) × 2(Anand) × 6(Remaining 3 boys) = 36.
Taking all the three cases together, there are 18 + 18 + 36 = 72 ways in which this can be done.