Exact same code giving TLE in c++ and AC in python3

I recently came across this problem on codeforces.

My soloution is bit different from what is given in the editorial here so let me explain it briefly. I created an array of size equal to the no. of stones and initialised 2 pointers left and right (initially left = 0 and right = sequence_string.length - 1) now I iterated over the sequence string, if the current letter is ‘l’ this means that the current stone should be the rightmost stone among all the reaming stones, so I’ll insert this stone to the array i.e array[right- -] = current_stone, similarly if current letter is ‘r’ than this stone should be the leftmost stone among all the remaining stones, i.e array[left++] = current_stone.

Coming to the issue, I submmited this code in c++ 14 and got time limit exceeded verdict, since I was sure that it is the correct algorithm I tried same code in python and it got accepted

c++ submission
python submission

I am not able to figure out the explanation for these verdicts, any help will be appreciated

Use new line character- ‘\n’ instead of endl. :white_check_mark:

1 Like