Its a programming challenge which I am trying to solve in JAVA