Solution seems fine to me. Please find where I am missing something.
link:- CodeChef: Practical coding for everyone