By the way, ChatGPT was able to solve this problem and give the correct solution.
It's in numerous algorithms textbooks and probably a lot of code repositories, so that's not surprising.
It's in numerous algorithms textbooks and probably a lot of code repositories, so that's not surprising.