-3

I have the following loop and wanted to understand the time complexity ...

for i = 1; i <= n; i++
    for j = 1; j <= n; j++
        j = j * i
    }
}
Praj
  • 49
  • 1
  • 5

2 Answers2

0

These for loops will run infinitely as j = j * i will always return 0 value for j when i=0.

If you are initializing i=1 then the complexity of these two loops will be O(n2) as explained in other comments/answers as it is nested loop.

Abhijeet Dhumal
  • 1,799
  • 13
  • 24
0

Inner Loop results in "stack overflow"

j value will always be 0 as it is multiplied by i whose value is 0.

So complexity will be O(INFINITY) .

If i is initialized from 1 i.e i=1 then it would result in some output whose complexity will be O(NlogN)

Viresh Mathad
  • 576
  • 1
  • 5
  • 19
  • can you justify why this would be NLogN. Just trying to learn and not challenge. – Praj Jul 29 '17 at 18:55
  • The variable in the inner loop is being multipled by a value "i" and incremented always which is constant for each iteration. So if there any loop variable is being multiplied/divided by a constant then it is considered as O(Log N) – Viresh Mathad Jul 29 '17 at 19:06