logic to solve this question "Problem 3" of contest "Code currents" ?

problem
link

And also please help me to understand the logic behind this solution
solution link