2
    int a = 0;
    for (int i = 0; i < n*n; ++i) {
        for (int j = 0; j <= i; ++j) {
            a += j;
        }
    }

Could someone help prove to me that this is function is $O(N^4)$.

I remember doing this problem a while back, but now I do not see it.

I understand that the outer loop is $O(N^2)$ but I am unsure of how this would cause the inner loop to be $O(N^2)$ as well

Thumbnail
  • 626
  • 3
  • 7
csguy
  • 21
  • 2

2 Answers2

2

Consider the range of j in the inner loop:

  • What is its maximum?
  • What is its average?
  • What is its average average?
Thumbnail
  • 626
  • 3
  • 7
-1

What does the big-Oh notation mean ? It always gives me an upper bound(worst case scenario) on the running time of an algorithm.

What is the worst-case running time of this algorithm ? It will happen when the outer loop (where i is defined) reaches its maximum value, and so j is subsequently forced to follow till i.

This should give you your answer.