Why is this time complexity O(n)? - complexity-theory

Why does the below function have a time complexity of O(n)? I can't figure it out for the life of me.
void setUpperTriangular (
int intMatrix[0,…,n-1][0,…,n-1]) {
for (int i=1; i<n; i++) {
for (int j=0; j<i; j++) {
intMatrix[i][j] = 0;
}
}
}
}
I keep getting the final time complexity as O(n^2) because:
i: execute n times{//Time complexity=n*(n*1)
j: execute n times{ //Time complexity=n*1
intMatrix[i][j] = 0; //Time complexity=1
}
}

The code iterates through n^2/2 (half a square matrix) locations in the array, so its time complexity is O(n^2)

This is same as insertion sort's for loop. Time complexity of insertion sort is O(n2).

So, CS department head explained it a different way. He said that since the second loop doesn't iterate n times, it iterates n! times. So technically it is O(n).

It can be at most considered as O(n.m) which finally comes down to O(n.n) or O(n^2)..

Related

Time complexity of an algorithm - embedded loop

I'm trying to solve this algorithm but I'm not sure
here is the code(trying to get the complexity)
For (i =0, i<N, i++) {
For (j=0, j<i/2, j++) {
S.O.P (“”);
}
}
the S.O.P stands for the instructions given to the CPU.
I don't know what S.O.P. stands for, but for the sake of the question, I'll suppose it takes a fixed amount of time - O(1).
Therefore, the only thing left is defining the time the for loops take to run.
for (i =0, i<N, i++) { // n times
For (j=0, j<i/2, j++) { // for each time, this runs n/2 times
S.O.P (“”); // fixed time O(1)
}
}
Given that, we can calculate:
T(n) = {sum for i from 0 to n} i/2 = (1/2)*(n*(n-1)/2)
T(n) = (n*(n - 1)/4) * O(1) = O(n^2/4) = O(n^2)
So the final time complexity would be O(n^2) (O of n squared).

What is the time complexity of this algorithm? It is a bit tricky

Suppose we have triple for loop, however, the O(1) statements within them are independent of the first loop so this for instance:
for (int i=1; i<=n; i++)
{
for ( int j=1; j<=20; j++)
{
for (int k=1; k<=5; k++)
{
//some statements independent of n
}
}
}
Since the statements are independent of n in the most inner for loop, wouldn't it just be O(n^2) as opposed to O(n^3)? Thanks!
Please, notice that inner loops don't depend on n:
for ( int j=1; j<=20; j++)
{
for (int k=1; k<=5; k++)
{
//some statements independent of n
}
}
that's why you can rewrite the the initial problem into
for (int i=1; i<=n; i++) {
//some statements independent of n
}
and you have O(n) complexity
It's O(n) with probably a larger constant. The two inner loops are fixed and independent of n.
It's O(n). Only one loop depends on n. Think about setting n to a very very high number. Than the others loop aren't really interesting.
Basically the total number of iterations that your code runs through is = N*20*5, which is basically equal to running N iterations, therefore your time complexity is O(N).

Time complexity of this algorithm with constant loop

I know that:
for(int i = 0; i < 1337; i++){
printf("\n");
}
Is O(1) since we have 1337 irrespective of n
But what about:
char * s = "abcdef";
for(int i = 0; i < 1337; i++){
strlen(s);
}
Is this O(N) now? Please any one explain
If you have a constant input the complexity of the algorithm will always be O(1) as you will perform the same number of operations.
If, however, s is input to your algorithm, the complexity will become O(len(s)). Note that O(N) has no meaning in this example as you have no not defined what is N.
You need to define what N is. Your first example is O(1) no matter how you view it. Your second example is O(N) in the length of s because strlen is O(N).

What's the Complexity of this Algorithm? BigO

I have the following algorithm:
for (i=0; i<=n-1; i++) {
for (j=i; j<=n-1; j++) {
sum = 0;
for (k=i; k<=j; k++) {
sum = sum + v[k];
if (sum > max) max = sum;
}
}
}
The complexity of the first is O(n), the second is n-i, the third is j-i+1.
I know O(n^3) is an upper bound. But what's the true thing we can assume as complexity of this algorithm? Is it O(n^3)?
Thank you.
I think O(n^3), the limits of the iterations doesnt matter.
It's O(n^3) in worst case (when i, j and k are of similar value). It's O(n) in best case, when j and k are 0 or 1:)
Since you have to be prepared for worst case data (and this is the main reason of examining complexity) you should assume O(n^3)

What is the time complexity of the following method in terms of Big-O notation?

Help me solve the time complexity of this method below here:
void method(int n, int[] array)
{
int i = 0, j = 0;
for(; i < n; ++i)
{
while(j < n && array[i] < array[j])
{
j++;
}
}
}
The runtime is O(n).
In some iterations of the outer loop the inner loop might progress several times and at other it might not progress at all, but in total there will be at most n increases of j. As this is independent of when (which values of i) this happens, you might say this is O(n) for the outer loop plus O(n) for the (up to) n increases of j. O(n) + O(n) = O(n).
This is contrary to the typical 'loop inside loop' which would perform n iterations of the inner loop for every iteration of the outer loop and thus be O(n) * O(n) = O(n^2).

Resources