Final solution added (Java)