Need help in solving this problem on codechef

here is the problem link
CodeChef: Practical coding for everyone, can someone please tell me the approach to solve this problem and intuition behind that approach.