Solution of second problem from hackerrank smvdu challenge1 in java