I am wondering if the complexity of this code is O(n) or is it O(count*n)? I made the parameter count and it is not dependent on the parameter n as you could see:
void change(int A[], int n, int x)
{
int i, j, count=0;
for(i=0; i<n; i++)
{
if(A[i]==x)
{ count++; }
}
for(i=0; i<count; i++){
for(j=0; j<n-i; j++){
printf("Hello World"):
}
}
}
The first loop is Theta(n)
. The time complexity of the second loop (as you've found depends on the value of count
) is:
T(n) = n + (n-1) + ... + (n-count) = O(n * count)
Therefore, the final time complexity is O(n * count)
. And as count = O(n)
, we can say the time complexity is O(n^2)
in terms of n
.