Why this code´s Big-O notation is not log(log(n)) - big-o

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):

Related

What is the complexity(Ø) of this pseudo code?

My question is about finding the complexity of this algorithm. J value is related to n, so I'm confused about this.
What is the asymptotic complexity of this pseudocode?
for i=1 to n
do
j = 1;
while (j < n)
do
j = j * 2;
Thanks.
I believe it is O(n log2n)
The outer loop is called n times and the inner loop is called log2n times, since in every iteration, j is doubled. For first iteration, i.e., k=0; j is equal to 1 and goes on like 2, 4, 8, ... until 2k>=n
If I add a print at the end of the inner loop, I see:
(1,2,5)
(1,4,5)
(1,8,5)
(2,2,5)
(2,4,5)
(2,8,5)
(3,2,5)
(3,4,5)
(3,8,5)
(4,2,5)
(4,4,5)
(4,8,5)
(5,2,5)
(5,4,5)
(5,8,5)
So it appears O(n^2) but the inner loop appears constant so probably O(n) -- If the (j < n) part is switched to (j < i), that would be closer to O(n log(n)):
(2,2,5)
(3,2,5)
(3,4,5)
(4,2,5)
(4,4,5)
(5,2,5)
(5,4,5)
(5,8,5)

Big O of Nested Loop (int j = 0; j < i; j++)

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).

what is the time complexity of this code and how? in Big-O

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).

Big O Notation queries

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).

complexity on a annidate for cycle

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)

Resources