0

What is the time complexity of this algorithm? I assume that is $O(3^n)$

n=3
recLoop(int n)
{
    for(int i=0; i<3; i++)
    {
        if(n>0)
        {
             recLoop(n-1); 
        }
        count++;
    }   
}

n 0 1 2 3 4


count 3 9 27 81 243

From this, I get $O(3^{n+1})$

If I change my algorithm to:

recLoop(int n)
{
    for(int i=0; i<3; i++)
    {
        for(int j=0; j<2; j++)
        {
            count++;
        }
    }
    for(int i=0; i<3; i++)
    {
        if(n>0)
        {
            recLoop(n-1);
        }
        else
            count++;
    }
}

From first nested loops $O(nm)$ so what is the total complexity of this algorithm?


n 0 1 2 3 4


count 9 33 105 321 969

The possible solution is a bit hard for me to understand and to translate my problem. I need a bit more description of my problem.

J. Doe
  • 51
  • 2
  • 8

0 Answers0