synchronizing Java and Scala versions of the problem