495 views
1 votes
1 votes
void xyz(char *s)
{
    if(s[0]=='\0')
        return;
    xyz(s+1);
    xyz(s+1);
    printf("%c",s[0]);
}

void main()
{
    xyz("123");
}

if the input string constant is of length $n$ then what is the time complexity?

  1. $O(2^n)$
  2. $O(n^2)$
  3. $O(n)$
  4. $O(n\log n)$

1 Answer

Best answer
3 votes
3 votes

Since in the output is of length 7 it means that there were 7 non trivial function calls in total which is (2$^{n}$ - 1) and hence the time complexity is O(2$^{n}$).


 

selected by

Related questions

1 votes
1 votes
0 answers
2
kartikeya2812 asked Jun 16, 2018
398 views
What will be the time complexity of the following algorithm ?A(n){if(n<=1) return 1;for(i=1;i<n;i++){ for(j=0;j<3;j++){ A(n-1) } }}
7 votes
7 votes
3 answers
3
bts asked May 29, 2018
1,879 views
Why is recursive equation of following code $T(n)=T(n/2)+O(1)$, not $T(n)=8*T(n/2)+O(1)$? int x=0; int A(n) { if(n==1) return 1; else { X+=8A(n/2)+n^3; } return X; }