TLE in CHEFSHIP(may cook off)

My code is giving tle and i am unable to figure out.it is passing all the sample test cases.
problem : https://www.codechef.com/COOK118B/problems/CHEFSHIP
My code : https://www.codechef.com/viewsolution/33350475
Please help!

Your code has a time complexity of O(N^2) and it cannot pass the given constraints in 1 sec which is why its showing TLE. You need to read the editorial for optimised solution or read others’ approach in problem editorial thread.

1 Like

substr(index,length) in the worst case operates in O(length), thereby giving a complexity of roughly o(n^2) to your approach which will not pass in 1 sec.

1 Like

Thanks! I got it

Thanks!