These are the Java algorithm on the example.