294 views
1 votes
1 votes

The time complexity of the below code is

int isPalindrome ( char * string, int n )
{
    if (n <= 1) return 1;
    if (string[0] != string[n-1]) return 0;
    return isPalindrome(string + 1, n-2);
}

 

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

1 Answer

Best answer
1 votes
1 votes
There is maximum $n$ recursive calls and each is of $O(1).$ So, total time complexity is $O(n).$

PS: The above code is not optimal in the sense that each characters at positions $i$ and $n-i-1$ are compared with each other twice.
selected by
Answer:

Related questions

3 votes
3 votes
1 answer
1
1 votes
1 votes
2 answers
2
3 votes
3 votes
1 answer
3
gatecse asked Aug 9, 2020
227 views
The cut vertices in the given graph areE and FA, C and DC, E and FB and C
1 votes
1 votes
1 answer
4