Maybe it's just a literal match (Codex is known to quote code verbatim [0]) for existing training data? Given the popularity of the problem in both CS classes and coding competitions, there's a good chance a matching implementation was present in its Github-based training data more than just a few times.
[0] https://thenewstack.io/this-week-in-programming-github-copil...