Solution of third problem of hackerrank SMVDU Challenge 1 in java