Knowee
Questions
Features
Study Tools

State The Big O Of The Following Fragment:For(I= 0; I < N; I++){   For(J = 1; J < N; J = J*2){      Cout << I << " ";   }}

Question

State The Big O Of The Following Fragment:For(I= 0; I < N; I++){   For(J = 1; J < N; J = J*2){      Cout << I << " ";   }}

🧐 Not the exact question you are looking for?Go ask a question

Solution

The outer loop runs N times and the inner loop runs log(N) times because J doubles with each iteration. Therefore, the time complexity of the fragment is O(N log N).

Similar Questions

What is the time complexity of the following code fragment?for(i=1; i<n; i= i*2)          a[i]=0;

What is the time complexity of the following code? for(int i = 0; i < n; i++) { for(int j = 0; j < i; j++) { System.out.println(i * j); } } a. O(n) b. O(2^n) c. O(n^2) d. O(n log n)

What is the time complexity of the following code :int i, j, k = 0;for (i = n/2; i <= n; i++) { for (j = 2; j <= n; j = j * 2) { k = k + n/2; }}

The time complexity of the  following code snippet isc=0; while (n>=1) {for(i=1;i<=n;i++){ c++;}n=n/2;}

Using big-O notation in terms of its parameter n, what is the running time of the below method in its worst case? Give the tightest and simplest bound possible, and justify your answer.public static int mystery(int n) {    int total = 0;    for (int i = 1; i < n; i *= 2) {        for (int j = i; j < 2 * i; j++) {            total += j;        }    }    return total;}

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.