Time complexity of Special Double For-Loop? - algorithm

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

Related

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)

Time Complexity of an Algorithm

Here is problem in which we have to calculate the time complexity of given function
f(i) = 2*f(i+1) + 3*f(i+2)
For (int i=0; i < n; i++)
F[i] = 2*f[i+1]
What i think is the complexity of this algorithm is O(2^n) + O(n) which ultimately is O(2^n).
Please correct me if i am wrong?
Firstly, all the information you required to work these out in future is here.
To answer your question. Because you have not provided a definition of f(i) in terms of I itself it is impossible to determine the actual complexity from what you have written above. However, in general for loops like
for (i = 0; i < N; i++) {
sequence of statements
}
executes N times, so the sequence of statements also executes N times. If we assume the statements are O(1), the total time for the for loop is N * O(1), which is O(N) overall. In your case above, if I take the liberty of re-writing it as
f(0) = 0;
f(1) = 1;
f(i+2) = 2*f(i+1) + 3*f(i)
for (int i=0; i < n; i++)
f[i] = 2*f[i+2]
Then we have a well defined sequence of operations and it should be clear that the complexity for the n operations is, like the example I have given above, n * O(1), which is O(n).
I hope this helps.

Complexity of algorithm

What is the complexity given for the following problem is O(n). Shouldn't it be
O(n^2)? That is because the outer loop is O(n) and inner is also O(n), therefore n*n = O(n^2)?
The answer sheet of this question states that the answer is O(n). How is that possible?
public static void q1d(int n) {
int count = 0;
for (int i = 0; i < n; i++) {
count++;
for (int j = 0; j < n; j++) {
count++;
}
}
}
The complexity for the following problem is O(n^2), how can you obtain that? Can someone please elaborate?
public static void q1E(int n) {
int count = 0;
for (int i = 0; i < n; i++) {
count++;
for (int j = 0; j < n/2; j++) {
count++;
}
}
}
Thanks
The first example is O(n^2), so it seems they've made a mistake. To calculate (informally) the second example, we can do n * (n/2) = (n^2)/2 = O(n^2). If this doesn't make sense, you need to go and brush up what the meaning of something being O(n^k) is.
The complexity of both code is O(n*n)
FIRST
The outer loop runs n times and the inner loop varies from 0 to n-1 times
so
total = 1 + 2 + 3 + 4 ... + n
which if you add the arithmetic progression is n * ( n + 1 ) / 2 is O(n*n)
SECOND
The outer loop runs n times and the inner loop varies from 0 to n-1/2 times
so
total = 1 + 1/2 + 3/2 + 4/2 ... + n/2
which if you add the arithmetic progression is n * ( n + 1 ) / 4 is also O(n*n)
First case is definitely O(n^2)
The second is O(n^2) as well because you omit constants when calculate big O
Your answer sheet is wrong, the first algorithm is clearly O(n^2).
Big-Oh notation is "worst case" so when calculating the Big-Oh value, we generally ignore multiplications / divisions by constants.
That being said, your second example is also O(n^2) in the worst case because, although the inner loop is "only" 1/2 n, the n is the clear bounding factor. In practice the second algorithm will be less than O(n^2) operations -- but Big-Oh is intended to be a "worst case" (ie. maximal bounding) measurement, so the exact number of operations is ignored in favor of focusing on how the algorithm behaves as n approaches infinity.
Both are O(n^2). Your answer is wrong. Or you may have written the question incorrectly.

Resources