Codathon 2020 hackerearth

Can anybody explain the logic behind the approach to this question of recently held codathon 2020 in hackerearth platform, they have published the solution code but not the editorial.

problem link