Solution for contest problems in Java