What is the asymptotic running time of the following piece of code? - algorithm

What is the asymptotic running time of the following piece of code?
if (N % 2 == 0) // N is even
for (int i = 0; i < N; i = i+1)
for (int j = 0; j < N; j = j+1)
A[i] = j;
else // N is odd
for (int i = 0; i < N; i = i+1)
A[i] = i;
If N is even we see the running time is O(n^2), when N is odd the running time is O(n). But I can't determine what the asymptotic running time is.
The possible answers are:
~ O(n)
~ O(n^2)
~ O(N * sqrt(N))
~ O(n log n)

There isn't a simple function you can use to asymptotically tightly bound the runtime. As you noted, the runtime oscillates between linear and quadratic at each step. You can say that the runtime is O(n2) and Ω(n), but without defining a piecewise function you can't give a Θ bound here.

Related

What is the time complexity of following nested dependent loops

for(int i = 2; i < N; i ++)
for(int j = 1; j < N; j = j * i)
sum += 1
I got
Can we generalize it further?
Using an algebraic identity about logarithms, logᵢ(N) = log N/log i, so we can take log N out as a factor and the summation is then of 1/log i. Approximating this summation as the integral of 1/log x, we get that asymptotically it is O(N/log N), per Wikipedia. Since we previously took out a factor of log N, multiplying by this gives a final result of O(N).

Find the Big O time complexity of the code

I am fairly familiar with simple time complexity regarding constant, linear, and quadratic time complexities. In simple code segments like:
int i = 0;
i + 1;
This is constant. So O(1). And in:
for (i = 0; i < N; i++)
This is linear since it iterates n+1 times, but for Big O time complexities we remove the constant, so just O(N). In nested for loops:
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
I get how we multiply n+1 by n and reach a time complexity of O(N^2). My issue is with slightly more complex versions of this. So, for example:
S = 0;
for (i = 0; i < N; i++)
for (j = 0; j < N*N; j++)
S++;
In such a case, would I be multiplying n+1 by the inner for loop time complexity, of which I presume is n^2? So the time complexity would be O(n^3)?
Another example is:
S = 0;
for (i = 0; i < N; i++)
for (j = 0; j < i*i; j++)
for (k = 0; k < j; k++)
S++;
In this case, I expanded it and wrote it out and realized that the inner, middle for loop seems to be running at an n*n time complexity, and the most inner for loop at the pace of j, which is also nxn. So in that case, would I be multiplying n+1 x n^2 x n^2, which would give me O(n^5)?
Also, I am still struggling to understand what kind of code has logarithmic time complexity. If someone could give me an algorithm or segment of code that performs at log(n) or n log(n) time complexity, and explain it, that would be much appreciated.
All of your answers are correct.
Logarithmic time complexity typically occurs when you're reducing the size of the problem by a constant factor on every iteration.
Here's an example:
for (int i = N; i >= 0; i /= 2) { .. do something ... }
In this for-loop, we're dividing the problem size by 2 on every iteration. We'll need approximately log_2(n) iterations prior to terminating. Hence, the algorithm runs in O(log(n)) time.
Another common example is the binary search algorithm, which searches a sorted interval for a value. In this procedure, we remove half of the values on each iteration (once again, we're reducing the size of the problem by a constant factor of 2). Hence, the runtime is O(log(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).

How is complexity of the following algorithm calculated?

int find_c(int n)
int i,j,c
for(i=1; i < 2*n; i=i*3)
for(j=400; j > 1; j--)
c++
for(i=c; i > 0; i--)
if(even(i/3))
for(j=n; j < n*n; j++)
c++
else
for(j=1; j < 900; j=j*3)
c++
return c
I have this algorithm which is written in java and i need to find it's complexity depending on the input parameter n in worst case scenario. I can understand the basics of this procedure in which each loop has to be viewed separately. The complexity of this algorithm should be n2 log n but i can't understand how it is calculated.
Can someone please explain this to me ?
Since this is some kind of excercise, I will just show some parts and let the rest for you. In particular look at this code:
if(even(i/3))
for(j=n; j < n*n; j++)
c++
else
for(j=1; j < 900; j=j*3)
c++
From the loop enclosing this we know that even(i/3) will be true approximately in half of the executions, so the then and the else part contribute in the same amount to the runtime.
Lets look now at the then part: j will run from n to n*n so order of O(n * n). The statements in the body of the loop have a runtime of O(1), so all together O(n * n).
The loop in the else part on the other hand will execute log3(900) times which is a constant and the runtime of the body is constant as well. So this loop will in total contribute a runtime of O(1).
In total we get therefore for the whole if a runtime of O(n * n) + O(1) which is just O(n * 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