BESTBATS : Getting Tle

I am getting a time limit exceeding error.Could someone tell me how to correct it? Thanks in advance.
Here is the link to my code :

I haven’t read the question statement. But i just had a look into your solution.

One obvious reason why your pgm gives TLE is because you have two “infinite-loops” in your pgm.


If a[k] happens to be equal to a[n-1], then the condition will always be true, and the pgm will never break out of this. There are two such instances in your code. Please try after rectifying them.