Give the worst case time complexity of the following two functions. Justify your answer.

void function1(int n){
     int i, k, sum=0;
     for(i=0; i<n; i++)
          if(i<n/2)
               sum=sum+1;
          else
               for(k=0;k<n;k++)
                    sum=sum-1;
}

void function2(int n){
     int i, j, k;
     for(i=0; i<n; i++)
     for(j=i; j<i*i; j++)
          if(j%i==0)
               for(k=0;k<j;k++)
                    printf("*");
}


Difficulty level
This exercise is mostly suitable for students
O(n^2)
O(n^4)

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Sorting using the radix sort algorithm