Difference in time taken by STL functions and simple code

I recently tried a question Censored

I got TIME LIMIT EXCEEDED(tle) but, after looking at the internal solution i realized that my attempt and the solution were doing the same thing and i guess the time complexity is similar. Then why am i getting a tle? Please explain

Thank You

Internal Solution

My code:

Try tmp += stk[top-i]

1 Like

It worked thank you. But how can this make a difference, are += and normal operations so different in terms of time of execution.

tmp += stk[top-i] takes constant time on average. tmp = tmp + stk[top-i] takes time proportional to the length of tmp.

2 Likes