Big-O of a function that has a nested loop affected by outer loop - big-o

for(int i = 0; i < N*N; i++) {
for(int j = 0; j < i; j++) {
//something O(1)
}
}
So I was trying to find big O of this function.
The outer loop is O(N^2) alone, and the inner loop goes from 0 to N^2. So I was thinking this would make the whole thing O(N^4). Am I correct in thinking this?

Yes, you are correct. The time complexity of the code is O(N^4).

Related

What is the time complexity for this nested for loop?

What would be the time complexity for the code below?
My guess is O(n log(n)) or O(n log(log(n))), but really not sure.
Would appreciate any help!
for (int i = 1; i < n; ++i) {
for (int j = 1; j < log (n); j *= 2) {
cout << '-';
}
}
it is clear that the first loop will run in
O(n)
the inner loop can be calculated as follows if look at log(n) (the stop condition of the loop) as k then it'll look like this
for(int j = 0; j < k; j *= 2)
the runtime of this for loop is
O(logk)
if we replace back log(n) instead of k we get the following
O(log(logn))
hence this is an inner loop we need to multiply its runtime with the outer loop runtime.
O(n) * O(log(logn)) => O(nlog(logn)).
Here you can read more about loops runtime and how to analyze it.

two or more for loops time complexity

If we assume the statements inside of a for loop is O(1).
for (i = 0; i < N; i++) {
sequence of statements
}
Then the above time complexity should be o(n)
another example is:
for (i = 0; i < N; i++) {
for (j = i+1; j < N; j++) {
sequence of statements
}
}
the above time complexity should be o(n^2)
It seems like one for loop symbolize n times
However, I've seen some discussion said that it's not simply like this
Sometimes there are 2 for loops but it doesn't mean the time complexity must be o(n^2)
Can somebody give me an example of two or more for loops with only O(n) time complexity (with explanation will be much better)???
It occurs when either the outer or inner loop is bounded by a fixed limit (constant value) on iterations. It happens a lot in many problems, like bit manipulation. An example like this:
for (int i = 0; i < n; i++) {
int x = ...; //some constant time operations to determine int x
for (int j = 0; x != 0; j++) {
x >>>= 1;
}
}
The inner loop is limited by the number of bits for int. For Java, it is 32, so the inner loop is strictly limited by 32 iterations and is constant time. The complexity is linear time or O(n).
Loops can have fixed time if the bounds are fixed. So for these nested loops, where k is a constant:
// k is a constant
for (int i=0; i<N; i++) {
for (int j=0; j<k; j++) {
}
}
// or this
for (int i=0; i<k; i++) {
for (int j=0; j<N; j++) {
}
}
Are both O(kN) ~= O(N)
The code in the post has a time complexity of:
Given this is a second order polynomial, (N^2 - N)/2, you correctly assessed the asymptotic time complexity of your example as O(N^2).

Run time and theta notation

for the following code:
for(i=0;i<5;i++)
for(j=2;j<n;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=a[i][k]*b[k][j];
}
I would say the run time is theta(n^3), as I see in the k loop, there is two n (n^3) and on the other loop, another n, making it n^3. Would this be right or what did I miss.
Thank you
Here is your code, formatted:
for (i=0; i < 5; i++) {
for (j=2; j < n; j++) {
c[i][j] = 0;
for (k=0; k < n;k++)
c[i][j] = a[i][k]*b[k][j];
}
}
The outer loop in i only iterates 5 times, and so can just be treated as a constant penalty as far as complexity is concerned. The inner two loops in j and k are independent of each other, and both are O(n). We can therefore just multiple the complexities to get O(n^2) for the overall running time as a function of n.

big O for an algorithm

I have a question about this algorithm, I am having hard time in understanding the complexity
While(input) {
...
array to check
...
for (int i=0; i< arraysize; i++) {
creation first subarray;
creation second subarray;
}
for (int i=0; i < firstsubarraysize; i++) {
addinput to array;
for( int j = 0; j < secondsubarraysize; j++) {
calculate array[j] * input;
}
}
}// endwhile
So, considering input a M variable and the array is N, the big O would be O(m(nlogn)) or O(n2)?
The sub arrays are not always n\2.
I apologize If I'm not very clear.
Generally, for each nested loop, you will have one N in the big O for time complexity.
Here assume the outer most loop while run M times, then there are two nested for inside of it. So the total is O(M N^2).

Big O Notation for a For-Loop

How do I find the Big O Notation for this for-loop line of code
for (int j = 0; pow(j,2) < n; j++) ?
Does anyone know?
I have read a little on Big O Notation and it’s a very confusing topic to understand. I know that usually for-loop like this one → for (int n = 0; n < 20; ++n), have a Big O notation of O(1), as input increases by 13 so does its output, with linear complexity. Is that the same situation as above?
A loop like this:
for (int i = 0; i < n; ++i) {
doSomething(i);
}
iterates n times, so if doSomething has O(1) running time, then the loop as a whole has O(n) running time.
Similarly, a loop like this:
for (int j = 0; pow(j, 2) < n; j++) {
doSomething(j);
}
iterates ⌈√n⌉ times, so if doSomething has O(1) running time, then the loop as a whole has O(√n) running time.
By the way, note that although pow(j, 2) is O(1) running time — so it doesn't affect your loop's asymptotic complexity — it's nonetheless pretty slow, because it involves logarithms and exponentiation. For most purposes, I'd recommend this instead:
for (int j = 0; j * j < n; j++) {
doSomething(j);
}
or perhaps this:
for (int j = 0; 1.0 * j * j < n; j++) {
doSomething(j);
}

Resources