-2

I'm kind of confused to conclude the Big-O notation for this while loop, where N is the input size:

 int array[0][(N-1)/2] = 1;
 int key = 2,k,l;
 i = 0;
 int j = (N-1)/2;
 while(key <= N*N)
  {
   if(i <= 0)
    k = N-1;
   else
    k = i-1;
   if(j <= 0)
    l = N-1;
   else
    l = j-1;
  if(array[k][l])
    i = (i+1)%N;
  else
    {
      i = k;
      j = l;
     }
  array[i][j] = key;
  key++;
   }
I concluded it as O(N2) 

because when N=5 it iterates until N*N i.e 5*5=25 times but I'm still confused regarding the rest of the code inside the loop. would really appreciate it if someone could give a step by step explanation of the code, and this loop is just part of a bigger function which has 4 more loops which i understood but not this loop.

shinwari_afg
  • 104
  • 11

1 Answers1

1

What you should actually care about is, how k changes. It grows by one in each iteration, and there are no shortcuts here.

So it's just O(N2).

dEmigOd
  • 528
  • 4
  • 8