I solved the problem using a different approach than the solutions given in editorial.
I’m getting answers and it’s working pretty well but the code is failing in some cases because I’m not able to provide enough space for the arrays. It is given in constraint that 0 <= n <=10^6. I tried to allot that space to both array and my 2D array but segmentation error is coming which I guess is due to buffer overflow of memory.
I need help to make it pass all the cases, how can I make it work for n <= 10^6? What changes should I make?
Here’s my solution: https://ide.codingblocks.com/s/593790