Let the distance from A to B is D km.
Distance for the entire journey is 2D km.
Time taken to go from A to B is D/30 hr and that of B to A is D/20 hr.
So, the total time taken T is
T = (D/30) + (D/20).
By solving, we will get,
T = D/12 hrs
Average speed = Total distance/Total time.
Average speed = 2D ÷ D/12
=> 2D x 12/D = 24 km/h.
Hence Average speed of the motorcycle is 24 km/h.