this is my codehttps://ide.codingblocks.com/s/307081 but i am only able to pass 1 out of 2 test cases.
Refer this discussion Hostel Visit- TLE This may help