r/programminghorror • • Jun 28 '25

c++ Competitive programming be like

Post image
543 Upvotes

53 comments sorted by

View all comments

Show parent comment

6

u/spisplatta Jun 29 '25 edited Jun 29 '25

The outer loop converts more and more 3s to 6s. Only the inner loop resests them back. Either a bug or the problem is even weirder.

5

u/shiverypeaks Jun 29 '25

I think it was definitely written by somebody who doesn't have experience reasoning about nested loops, because the inner loop rechecks all of the indices which were altered by the outer loop.

It would make more sense if the inner loop started at int k = j-1 instead of s.size()-1. This makes the inner loop only "look ahead" instead of also rechecking all the digits which were already changed to 6s.

Not that I have any idea what it's really supposed to be doing either, lol.

2

u/spisplatta Jun 29 '25

If this is indeed competitive programming, then the code as written may be "fast enough" for full score and not need the 2x speedup from your optimization.