Simple explanation needed

link to the problem: Programming tutorials, Coding problems, and Practice questions

OR the same problem on CodeChef platform link (which has no editorial unlike HackerEarth): CodeChef: Practical coding for everyone

Editorial given is not much explained and code is also terrible. Can anyone explain the solution to it?