C++ implementation work exactly the same as Java code.
I win!