Can you optimize this code? I am trying to run it but it shows code took too long to run. Working fine in local machine.