Chefina And Swap

Guys can anyone help me to improve this code so that I don’t get TLE

Question

Solution