Algorithm by Java.