# TLE arising in spite of O(n) for constraints ranging upto 10^5

Both are having a time complexity of O(n) but still one is accepted and the other one is not. I can’t spot the discrepancy occurring on the above two solution.

Any suggestions are warmly welcome.

This:

      res=s[i]+res;


line is O(|\text{res}|) per call. Since it is called O(\frac{K}{2})=O(N) times (K can be as large as N) and res grows by one each time, this loop:

 while(i<k/2){
res=s[i]+res;
res=s[k-i-1]+res;
i++;
}


is O(N^2).

Edit:

I haven’t tested it, but this small tweak to your original TLE code might fix it: okcoder30-RMNTREV-TLE-Fix - Diff Checker