Loops Hackerrank Solution In Java