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); } $\Theta(n)$ $\Theta (n \log n)$ $\Theta(\log n)$ $\Theta(n^2)$ DS go2025-ds-1 time-complexity + – gatecse asked Aug 9, 2020 gatecse 294 views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
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. gatecse answered Aug 9, 2020 • selected Aug 8, 2021 by Arjun gatecse comment Share Follow See all 4 Comments See all 4 4 Comments reply niha42 commented Jan 5, 2021 reply Follow Share What would be the recurrence relation for the number of calls? 0 votes 0 votes the_psycho_scientist commented Jan 14, 2021 reply Follow Share T(n) = T(n-2) + c Is this the correct recurrene relation for the above function? 2 votes 2 votes Aaryan_Sharma commented Dec 30, 2022 reply Follow Share same doubt 0 votes 0 votes Shalini Sil commented Jul 29, 2023 reply Follow Share can anyone provide an answer to this 0 votes 0 votes Please log in or register to add a comment.