int k = n;
while (k > 0)
{
for (int j = 0; j < n; j++)
{
System.out.println(“Inside the inner loop”);
}
k = k / 2;
}
Hi for this question, I came up with two answers, that is
O (N^2 Log N) OR O (n * N/2) = O (N2/2). I'm not sure if there are the same or different? My lecturer also mentioned to chose the upper case. Therefore, for this question, there are one O(N^2) and one O(Log N). So the answer should be O(N^2) according to my lecturer? please help. Thanks.
The answer is O(N * log N)
Since you divide K in halfes you get O(log N) for outer loop. And since in each iteration you iterate N times then it will be O(N * log N).
Related
tomorrow we have midterm exam and we cant solve the Big O notation of this code:
for(int i =1; i<n;i=i*2)
{
for(int j=1; j<i; j = j*2)
{
cout << "hello";
}
}
We think it can be "log(log(n))" but it´s not true.
This loop:
for(int i =1; i<n;i=i*2)
will execute its body with i in 1, 2, 4, 8, ..., up to (but not including) i = n. So it will execute its body O(log n) times.
This loop:
for(int j=1; j<i; j = j*2)
will execute its body with j in 1, 2, 4, 8, ..., up to (but not including) i = j. So it will execute its body O(log(j)) times. Because j < n, O(log j) < O(log n), so we can also say the j loop is O(log n).
We've established that the i loop performs O(log n) executions of the j loop, and the j loop performs O(log n) executions of its body. Therefore we multiply the factors to get O(log n) * O(log n) = O(log n * log n) = O(log² n) total executions of the j loop's body.
Note that log² n is the mathematician's way of writing (log n)². It is not the same as log log n.
If we make a graphic table, we can see the answer is O(n^2):
for (int i = n; i > 0; i /= 2) {
for (int j = 0; j < i; j++) {
//statement
}
}
Answer: O(N)
I know that the first loop for for (int i = n; i > 0; i /= 2) results in O(log N).
The second loop for (int j = 0; j < i; j++) depends on i and will iterate first i times then i / 2, i / 4, ... times. (where i depends on n)
I don't know the Big O for the second loop, but I thought the answer would be O(log N * something) where O(log N) is the outer loop and something is the inner loop?
How do you get O(N)?
The outer loop has a complexity of O(log n), because of the i /= 2. But the inner loop is a little bit more tricky.
The inner loop has a complexity of O(i), but the i is changing for each iteration of the outer loop. In combination with the outer loop you get a complexity of O(n / log n). You get this as follow:
The number of steps, the inner loop is doing, is similar to the sum of 1/(2n), as described on https://en.wikipedia.org/wiki/1/2_%2B_1/4_%2B_1/8_%2B_1/16_%2B_⋯. At first you are doing n steps, then only n/2 steps, then n/4 steps and so on until you do only 2 steps and then finally 1 step. This sums up together to the result of 2n. In total you run the inner loop log n times (as defined by the outer loop). This means the inner loop runs at average 2n / log n times. So you have a complexity of O(n / log n).
With the outer loop of O(log n) and the inner loop of O(n / log n) you get O(log n * n / log n), which can be simplified to O(n).
int i, j, k = 0;
for (i = n/2; i <= n; i++) {
for (j = 2; j <= n; j = j * 2) {
k = k + n/2;
}
}
I came across this question and this is what I think.
The outer loop will run, N/2 times and the inner loop will run logN times so it should be N/2*logN. But this is not the correct answer.
The correct answer is O(NlogN), can anybody tell me what I am missing?
Any help would be appreciated.
Let's take a look at this block of code.
First of all, you can notice that inner loop doesn't depend on the external, so the complexity of it would not change at any iteration.
for (j = 2; j <= n; j = j * 2) {
k = k + n/2;
}
I think, your knowledge will be enough to understand, that complexity of this loop is O(log n).
Now we need to understand how many times this loop will be performed. So we should take a look at external loop
for (i = n/2; i <= n; i++) {
and find out, that there will be n / 2 iterations, or O(n) in a Big-O notation.
Combine these complexities and you'll see, that your O(log n) loop will be performed O(n) times, so the total complexity will be O(n) * O(log n) = O(n log n).
So I was just asked this question in an exam, and it's driving me crazy. The question is this:
What is the time complexity for the following code in terms of n:
int count = 0;
for(int i = 0; i < n; i++) {
for(int j = 1; j < n; j = j * 2) {
count++;
}
}
a) O(n log(n))
b) O(n^2)
I firmly believe the answer to be n(log(n)), because the inner loop only runs k times, where k^2 <= n, which is only log2(n), for which the time complexity is log(n). However, everyone I have talked to who was also in the exam thinks the answer to be n^2. Can anyone give me a firm reasoning for either way? Thank you!
Your reasoning is correct with minor edit below about the answer as O(n log n). There is no way the answer can be O(n^2).
2^k = n so k = log n
The inner loop will run: log n times
The outer loop will run: n times
So this is O(n log n) time complexity
i would like to know if the solution for the complexity of that code is correct:
for (j = 2^N; j>1; j = j/2) {
h = h * 2;
for (i =1; i < j; i = i*2)
for (k=2; k<log N; k++)
cont ++;
}
According me the last cycle have complexity log n
The first cycle have complexity n
The second cycle have complexity log n
So the total complexity is n log n
Best Regards
You have three loops here:
First is linear in N (logarithmic in 2^N)
Second is linear in N (logarithmic in 2^N)
Third is logarithmic in N
So the whole code looks rather as O(N^2 log N)