Link for my solution link : MySolution.

And for the above solution, I have just edited my partially accepted solution (Partially Accpeted.). Please help me to identify the problem. I have already tried to increase the size of the dynamically allocated array but same result and also tried to change the pointers to the static arrays for both **d** and **sp** in mine solution. I was using Sparse Matrix concept.

On your local machine, was your code running in less than 3s for all 1s in 300x300 matrix?

It shouldn’t ideally…

Let’s say every cell has a 1, so your if condition is entered n^2 times worst case and inside the **if** there are 2 nested loops again leading to another n^2 complexity factor getting multiplied to you total complexity which becomes **n^2 x n^2 = n^4**. So if n = 300, total iterations can go upto 8x10^9 which is not feasible(I hope you know that, 1 sec = 10^8 iterations approx).

Refer this link for tutorial.