Document Details

SuaveSugilite8165

Uploaded by SuaveSugilite8165

Anil Neerukonda Institute of Technology and Sciences

Tags

java recursion programming algorithms computer science

Summary

This document provides examples of Java recursion, including factorial calculation, printing from n to 1, and more. It also includes practice questions and a presentation style for teaching recursion.

Full Transcript

Recursion What and Why? Function calls Function calls Function calls Factorial Recurrence Relation Ques: Q1 : Make a function which calculates the factorial of n using recursion. Ques: Q1 : Make a function which calculates the factorial of n using recursion. Ques: ⑭ Q1 : Make...

Recursion What and Why? Function calls Function calls Function calls Factorial Recurrence Relation Ques: Q1 : Make a function which calculates the factorial of n using recursion. Ques: Q1 : Make a function which calculates the factorial of n using recursion. Ques: ⑭ Q1 : Make a function which calculates the factorial of n using recursion. 120 n = S factl 5) d 24 * 5 fact (4) d6 fact (3) * 4 ↓ 2 * 3 fact (2) d I * 2 fact(1) Base case Ques: Q2 : Print n to 1 Ques: Q2 : Print n to 1 Ques: Q2 : Print n to 1 Ques: Q3 : Print 1 to n (extra parameter) Ques: Q3 : Print 1 to n (extra parameter) Ques: Q4 : Print 1 to n (after recursive call) Ques: Q4 : Print 1 to n (after recursive call) Ques: Q5 : Print sum from 1 to n (Parameterised) Ques: Q5 : Print sum from 1 to n (Parameterised) Ques: Q6 : Print sum from 1 to n (Return type) Ques: Q7 : Make a function which calculates ‘a’ raised to the power ‘b’ using recursion. Ques: Q7 : Make a function which calculates ‘a’ raised to the power ‘b’ using recursion. Ques: Q8 : Power function (logarithmic) Ques: Q8 : Power function (logarithmic) Ques: Q8 : Power function (logarithmic) *Multiple Calls Q9 : Write a function to calculate the nth fibonacci number using recursion. [Leetcode 509] - 3 /tibols)t &.... - - 2 fibo (3) tiboly, de irtboli 2 L fibo (2) fibo(3) t d --Y 1 fibo (2) t I fibo(l) boo fid , os find 0 Euler's - Tour Free Ques: Q3 : Stair Path 5 = 8 Ways- · ways - n = 5 Find no of ways to Y I I 1 I I nt reach stair if 11 12 3 at I or 2 jump a 112/ 2 / time is allowed 121 2111 I ④ 212 221 Ground I 22 Ques: Q3 : Stair Path Ques: Q3 : Stair Path Ques: I Q3 : Stair Path Homeworair 1 2 I Deemp I lop R Top & stair (2) = 2 Stair (1) = 1 Ques: Q4 : Maze path I 2 3 I 2 3 I 2 3 I * I * I * 2 2 2 3 * 3 * 3 * RRDD RDRD RDD R I 2 3 I 2 3 I 2 3 I * I * I * 2 2 2 3 * 3 * 3 * DDRR DRDR DRRD Ques: Q4 : Maze path Ques: Q4 : Maze path Ques: Q4 : Maze path I - - t MXM MXm 1 = m-1Xh Pre In Post Pre In Post Call Stack pip(3) I d I 3 2 11 12 111 232 11 12 11123 O f O pip /2) I ↓ 2 11 12 111 2 pipLI) pip(0) L 1 11 base case - nothing Ques: Q5 : Print zig-zag Input Output 1 111 2 211121112 3 321112111232111211123 4 432111211123211121112343211121112321112111234 Traversing an array using recursion Print all the elements of an array Skip a character Skip /0 , "Raghar" , "") Al Remove all occurrences of ‘a’ from a string. skip) 1 "Raghaw" "1") C , * "Raghar" ; skip (2 "Raghar" "R") , , Strings = A "Rghr" ; String skip /3 "Raghar" Rg I ans = , , * skip (4 , Raghav , Rgh) * = O(n) skip /S , Raghav Rgh) , * skip (6 , Raghaw , Rghv) Subsets Print subsets of a string with unique characters. Follow Up : Do it for array as well [Leetcode 78] Subsets Print subsets of a string with unique characters. Follow Up : Do it for array as well [Leetcode 78] IIII s = abs a X i ↳ a A ⑤ ~ d L b //// ab deFara , c C d Y abo 2 + 4 + 8 Enc = 2 + 22 + 13.... -2 2. - 0(2") Ques: Q6 : Print all increasing sequences of length k from first n natural numbers. Permutations Finding all permutations of an string given all elements of the string are unique. Greatest Common Divisor Calculate Greatest Common Divisor of two numbers. Greatest Common Divisor Calculate Greatest Common Divisor of two numbers. 450L a Tb ⑫ - -a Ta 4 d ↑ TsL hof (a b) = hcf (b % a a) , & , d hof E Greatest Common Divisor Calculate Greatest Common Divisor of two numbers. Practice Generate all binary strings of length n without consecutive 1’s Practice Generate all binary strings of length n without consecutive 1’s Ques: Q2 : Generate Parentheses [Leetcode 22] Ques: Q2 : Generate Parentheses [Leetcode 22] Ques: Q2 : Generate Parentheses [Leetcode 22] Ques: Q3 : Kth Symbol in Grammar [Leetcode 779] Ques: Q4 : Count and Say s = 33222 s = 31 Imodity Imodity 1332 ans = 23 3211 [Leetcode 38] Ques: Q4 : Count and Say [Leetcode 38] Ques: Q4 : Count and Say while (i < n)E 0 123456789 chanAt(i) Ii s = 332225/ 11/ if/S. charAt(i) = = S i j j ++i ised s 23 32154/ len j i I↳ and = = - ans += len ane += 3. channelis [Leetcode 38] THANK YOU

Use Quizgecko on...
Browser
Browser