Unable to understand the editorial

Though I had tried to solve this coding challenge for 2 hrs and passed only a few testcases…
Then I looked at the editorial…
Editorial is too Complex to understand…
Please teach me about its logic…

Hi
contact me on 8750354215