SLIDE 1
Recitation ¡3 ¡(Recursion ¡+ ¡time ¡complexity) ¡
¡ Question ¡ 1: ¡ A ¡ palindrome ¡ is ¡ a ¡ sequence ¡ of ¡ characters ¡ or ¡ numbers ¡ that ¡ looks ¡ the ¡ same ¡ forwards ¡ and ¡
- backwards. ¡For ¡example, ¡"dennis ¡sinned" ¡is ¡a ¡palindrome ¡because ¡it ¡is ¡spelled ¡the ¡same ¡reading ¡it ¡from ¡
front ¡to ¡back ¡as ¡from ¡back ¡to ¡front. ¡The ¡number ¡12321 ¡is ¡a ¡numerical ¡palindrome. ¡Write ¡a ¡function ¡that ¡ takes ¡a ¡string ¡and ¡its ¡length ¡as ¡arguments ¡and ¡recursively ¡determines ¡whether ¡the ¡string ¡is ¡a ¡palindrome: ¡ ¡
bool isPalindrome(const char *str, const int len){ if (len <= 1) return true; return ((str[0] == str[len - 1]) && isPalindrome(str + 1, len - 2)); }
What ¡is ¡the ¡time ¡complexity ¡of ¡this ¡function ¡in ¡terms ¡of ¡Big-‑O ¡notation? ¡ ¡ Answer: ¡O(len) ¡ ¡ ¡ Question ¡2: ¡(Old ¡midterm ¡question) ¡Consider ¡the ¡problem ¡of ¡finding ¡the ¡k ¡element ¡subsets ¡of ¡a ¡set ¡with ¡n ¡
- elements. ¡Write ¡a ¡recursive ¡function ¡that ¡takes ¡an ¡array ¡of ¡integers ¡representing ¡the ¡set, ¡the ¡number ¡of ¡