I recently solved a question where my algo has complexity of O(N).

Surprisingly, on constraint where N<=1000, Solution takes 0.97 seconds ; but on constraint where N<=100000 ;
it takes 0.00 seconds (C++ Solution). Why is that happening…??? And even why at a constraint of N<=1000 ; solution is taking nearly 1 second time even in C++ ?

Whenever adding a character to a string use ‘+=’ operation i.e.

ans += “B”
instead of
ans=ans+“B”

First operation just adds character B to the string whereas the second operation makes a copy of first string, adds character and then assigns to the original string. Therefore your code might look like O(n) but its not.

But one extra thing @raj_13 … Could you please help me understand that if my code had a complexity of O(N) ; and constraints N<=100000 ; why even the run time goes nearly to 1 second…???

I am trying to shift to C++ because it is way faster than python. But even that seems quite slow…