Here is the link to the problem : Your text to link here... The problem tag says that it is a math related problem,but I am not able to find the logic behind it. I did find a solution on geeksforgeeks for a similar type of problem but the implementation seems a bit vague since there is no explanation provided as to how the logic was deduced. Here is the link for gfg sol : Your text to link here... It would be of great help if someone could explain me the logic behind this solution.