-5
\$\begingroup\$

I use this for reversing a string, but it has O(n) :

reverse(y.begin(),y.end());

How can I reverse a string in O(n/2) ?

\$\endgroup\$
  • 2
    \$\begingroup\$ Welcome to Code Review. Please see How do I ask a good question? While not necessarily off-topic, your question's scope is so limited (1 single expression) that there is only very little we can actually review. \$\endgroup\$ – Phrancis Apr 6 '16 at 20:21
  • \$\begingroup\$ Being too short has never been a reason to close. If you think there's no useful advice you can note that, but the question is not off topic. \$\endgroup\$ – SuperBiasedMan Apr 7 '16 at 8:42
  • 3
    \$\begingroup\$ I'm voting to close this question as off-topic because you're not looking for a code review and just an alternative solution on a black-box level ("gimme the better codes"). \$\endgroup\$ – Pimgd Apr 7 '16 at 9:11
  • \$\begingroup\$ @Pimgd I'm not convinced that it is off-topic. The question is basically asking "How can I do it faster?" The answer is, essentially, "Your code is fine." \$\endgroup\$ – 200_success Apr 8 '16 at 14:25
  • \$\begingroup\$ @200_success OP would not be helped by a review talking about that poor variable naming, what is y supposed to mean? They're looking for an alternative code snippet that will replace this one. \$\endgroup\$ – Pimgd Apr 8 '16 at 14:34
5
\$\begingroup\$

You did it already: \$O(\frac{n}{2})\$ is exactly the same as \$O(n)\$.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.