Not able to get the logic

Hello …
I am trying to solve this question from hackerearth…

I am not able to understand whole editorial…I am trying to understand one guy submission which is somewhat similar to editorial.https://www.hackerearth.com/submission/43754745/

Could you explain please… What he is doing?Thanks

Hey @ashishnnnnn
Refer to comments

Do a dry run along with it on some testcase

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.