The solve function is running infinitely i guess.. but why?

check chat option , need to discuss about approach