It doesn’t produce the correct graph for N=9.
I think what you’re trying to do is essentially what i did . Have a Look
@sarvagya3943 , yeah i got that now , i don’t know what was going on in my mind when i was solving that question during contest . feeling very stupid