The distance that the train(slower one) will have to travel will be equal to the length of the train as the position of the driver is a point ,
Distance (S) = 500 m = 0.5 km
And the speed will be the relative speeds of the trains
Relative speed(v) = 45+30 (As the they are in opposite direction) = 75 km/hr
time = 0.5 / 75 = 1/ 150 hours = 1/150 × 3600 = 24 seconds .
I hope it is correct !
Please mark as the best if u are satisfied !