├── .gitignore ├── README.md ├── Recursion Session 1 ├── Intro Problems.bin ├── After creating Mantra.bin ├── .cph │ ├── .Intro Problems.cpp_28efd46c5873d913e2e5705f1df7eae8.prob │ └── .After creating Mantra.cpp_441a11d23c808d7f196eec2942748f4a.prob ├── Intro Problems.cpp └── After creating Mantra.cpp ├── temp.py ├── Backtracking Session-1 ├── Permutations │ ├── backtracking_solution.cpp │ └── recursive_solution.cpp ├── letter_combination.cpp ├── Subsets │ ├── subsets-2.cpp │ └── subsets-1.cpp ├── diceRoll.cpp └── maze.cpp ├── Recursion Session 2 ├── josephus.cpp ├── toh.cpp ├── n Bit Binary.cpp ├── subsetSum.cpp ├── next happy number.cpp ├── letter combination of phone number.cpp ├── getSubsequence.cpp └── AssignmentSolutions.cpp ├── .cph └── .E_Assiut_Chess.cpp_266ffaab4b94daac803595c1d09125a9.prob ├── Backtracking Assignment.md ├── .vscode └── launch.json ├── Recursion Assignment.md └── Assignment.md /.gitignore: -------------------------------------------------------------------------------- 1 | .cph 2 | .vscode -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Recursion-and-BackTracking-Live-Sessions 2 | -------------------------------------------------------------------------------- /Recursion Session 1/Intro Problems.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Girl-Code-It/Recursion-and-BackTracking-Live-Sessions/HEAD/Recursion Session 1/Intro Problems.bin -------------------------------------------------------------------------------- /Recursion Session 1/After creating Mantra.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Girl-Code-It/Recursion-and-BackTracking-Live-Sessions/HEAD/Recursion Session 1/After creating Mantra.bin -------------------------------------------------------------------------------- /temp.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | if(pos == len(s)-1) {print(s); return;} 4 | 5 | for(i = pos; i < len(s); i+=1){ 6 | swap(s[i],s[pos]); 7 | f(s,pos+1); 8 | swap(s[i],s[pos]); 9 | } -------------------------------------------------------------------------------- /Backtracking Session-1/Permutations/backtracking_solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void permute(string &s){ 5 | } 6 | 7 | int main(){ 8 | string s = "abc"; 9 | permute(s); 10 | } 11 | -------------------------------------------------------------------------------- /Recursion Session 2/josephus.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/josephus-problem/1 2 | 3 | #include 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | int josephus(int n, int k) 10 | { 11 | if (n == 1) 12 | return 1; 13 | else 14 | return (josephus(n - 1, k) + k - 1) % n + 1; 15 | } 16 | }; 17 | 18 | -------------------------------------------------------------------------------- /Backtracking Session-1/letter_combination.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector charArray={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; 5 | //0 1 2 3 4 5 6 7 8 9 6 | 7 | void letter(string &digits){ 8 | } 9 | 10 | int main(){ 11 | string digits = "258"; 12 | letter(digits); 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Recursion Session 1/.cph/.Intro Problems.cpp_28efd46c5873d913e2e5705f1df7eae8.prob: -------------------------------------------------------------------------------- 1 | {"name":"Local: Intro Problems","url":"d:\\Manvi\\GCI\\Recursion and Bactracking Sessions\\Recursion Session 1\\Intro Problems.cpp","tests":[{"id":1628587735424,"input":"","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"d:\\Manvi\\GCI\\Recursion and Bactracking Sessions\\Recursion Session 1\\Intro Problems.cpp","group":"local","local":true} -------------------------------------------------------------------------------- /Recursion Session 1/.cph/.After creating Mantra.cpp_441a11d23c808d7f196eec2942748f4a.prob: -------------------------------------------------------------------------------- 1 | {"name":"Local: After creating Mantra","url":"d:\\Manvi\\GCI\\Recursion and Bactracking Sessions\\Recursion Session 1\\After creating Mantra.cpp","tests":[{"id":1628593995968,"input":"","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"d:\\Manvi\\GCI\\Recursion and Bactracking Sessions\\Recursion Session 1\\After creating Mantra.cpp","group":"local","local":true} -------------------------------------------------------------------------------- /Recursion Session 2/toh.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void toh(int n, int from, int to, int helper) { 5 | if(n==1){ 6 | cout<<"moving disc " < 4 | using namespace std; 5 | 6 | class Solution{ 7 | public: 8 | void rec(vector& ans, int N,string curr, int nz, int no){ 9 | if(N==0){ 10 | ans.push_back(curr); 11 | return; 12 | } 13 | if(no>nz){ 14 | rec(ans,N-1,curr+"0",nz+1,no); 15 | } 16 | rec(ans,N-1,curr+"1",nz,no+1); 17 | } 18 | vector NBitBinary(int N) 19 | { 20 | vector ans; 21 | rec(ans,N,"",0,0); 22 | 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /Backtracking Session-1/Subsets/subsets-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void subsetHelper(vector &nums, vector &chosen, int startIdx){ 5 | for(auto &e : chosen) cout << e << " "; 6 | cout << "\n"; 7 | 8 | //base case 9 | if(startIdx == nums.size()){ 10 | return; 11 | } 12 | 13 | for(int i = startIdx; i < nums.size(); i++){ 14 | chosen.push_back(nums[i]); 15 | subsetHelper(nums,chosen,i+1); 16 | chosen.pop_back(); 17 | } 18 | } 19 | 20 | void printSubsets(vector &nums){ 21 | vector chosen; 22 | subsetHelper(nums,chosen,0); 23 | } 24 | 25 | int main(){ 26 | vector nums = {1,2,3,4}; 27 | printSubsets(nums); 28 | } -------------------------------------------------------------------------------- /Recursion Session 2/subsetSum.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/subset-sums2234/1 2 | 3 | 4 | #include 5 | using namespace std; 6 | 7 | // try solving it using the method we discussed during the session as well. 8 | class Solution{ 9 | public: 10 | void subsets(vector&arr,int N,int index,int sum,vector&v){ 11 | if(index==N){ 12 | v.push_back(sum); return; 13 | } 14 | 15 | subsets(arr,N,index+1,sum+arr[index],v); 16 | 17 | subsets(arr,N,index+1,sum,v); 18 | } 19 | 20 | vector subsetSums(vector arr, int N) 21 | { 22 | vectorv; 23 | subsets(arr,N,0,0,v); 24 | sort(v.begin(),v.end()); 25 | return v; 26 | } 27 | }; -------------------------------------------------------------------------------- /Recursion Session 1/Intro Problems.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | void print1toN(int n){ // 1,2,3,4,5 6 | if(n == 0) return; 7 | print1toN(n-1); //1,2,3,4 8 | cout << n << " "; //5 9 | } 10 | 11 | void printNto1(int n){ //5 4 3 2 1 12 | if(n == 0) return; 13 | cout << n << " "; //5 14 | printNto1(n-1); //4 3 2 1 15 | } 16 | 17 | // 5! = 5*4*3*2*1 18 | int factorial(int n){ 19 | if(n==0) return 1; 20 | int small_ans = factorial(n-1); //4*3*2*1 = 24 21 | int ans = n*small_ans; 22 | return ans; 23 | } 24 | 25 | 26 | int main(){ 27 | cout << "1 to N\n"; 28 | print1toN(5); 29 | 30 | cout << "\n\nN to 1\n"; 31 | printNto1(5); 32 | 33 | cout << "\n\nFactorial\n" << factorial(5); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Backtracking Session-1/Permutations/recursive_solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Question: 4 | input: abc 5 | output: 6 | abc 7 | acb 8 | bac 9 | cab 10 | bca 11 | cba 12 | 13 | Recursive Thinking: 14 | 1. Base case 15 | 2. Smaller problem - Recursive Work - Leap of Faith 16 | 3. My work 17 | 18 | substr(i,k) = starting from index i, string of length k, i.e substring(i,i+k-1) 19 | substr(k) = substring from index k till end 20 | 21 | s = CODING 22 | 012345 23 | 24 | s.substr(2,3) = DIN 25 | s.substr(2) = DING 26 | 27 | */ 28 | 29 | #include 30 | using namespace std; 31 | 32 | //osf = output so far 33 | 34 | void permute(string ip){ 35 | 36 | } 37 | 38 | int main(){ 39 | permute("abc"); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Recursion Session 2/next happy number.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/next-happy-number4538/1/ 2 | 3 | 4 | #include 5 | using namespace std; 6 | class Solution { 7 | public: 8 | bool check(int N){ 9 | if(N<=10){ 10 | if(N==1 || N==7 || N==10){ 11 | return true; 12 | } 13 | return false; 14 | } 15 | 16 | 17 | int sum=0; 18 | while(N>0){ 19 | int t=N%10; 20 | sum+=(t*t); 21 | N/=10; 22 | } 23 | return check(sum); 24 | } 25 | int nextHappy(int N){ 26 | for(int i=N+1; ; i++){ 27 | if(check(i)){ 28 | return i; 29 | } 30 | } 31 | return 0; 32 | } 33 | }; -------------------------------------------------------------------------------- /Backtracking Assignment.md: -------------------------------------------------------------------------------- 1 | Done in Session 1: 2 | 1. https://www.codestepbystep.com/problem/view/cpp/backtracking/diceRolls 3 | 2. https://leetcode.com/problems/subsets/ 4 | 5 | 6 | Todos for remaining Sessions: 7 | 1. https://practice.geeksforgeeks.org/problems/rat-in-a-maze-problem/1 8 | 2. https://leetcode.com/problems/n-queens/ 9 | 3. https://leetcode.com/problems/sudoku-solver 10 | 4. https://leetcode.com/problems/all-paths-from-source-to-target 11 | 5. https://leetcode.com/problems/path-with-maximum-gold 12 | 6. https://leetcode.com/problems/target-sum 13 | 7. https://leetcode.com/problems/partition-to-k-equal-sum-subsets 14 | 15 | 16 | Take-Home Assignment: 17 | 1. https://leetcode.com/problems/combinations 18 | 2. https://leetcode.com/problems/generate-parentheses 19 | 3. https://leetcode.com/problems/letter-tile-possibilities 20 | 4. https://leetcode.com/problems/letter-case-permutation 21 | 5. https://leetcode.com/problems/permutations-ii 22 | 6. https://leetcode.com/problems/subsets-ii 23 | 7. https://leetcode.com/problems/combination-sum-ii 24 | 8. https://leetcode.com/problems/letter-combinations-of-a-phone-number 25 | -------------------------------------------------------------------------------- /Recursion Session 2/letter combination of phone number.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 2 | 3 | #include 4 | using namespace std; 5 | 6 | class Solution { 7 | public: 8 | vector charArray={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; 9 | vector rec(string& digits){ 10 | if(digits.size()==0){ 11 | vector base; 12 | base.push_back(""); 13 | return base; 14 | } 15 | 16 | string smallString=digits.substr(1); 17 | 18 | vector smallAns=rec(smallString); 19 | 20 | vector myAns; 21 | char ch=digits[0]; 22 | 23 | string str=charArray[ch-'0']; 24 | 25 | for(int i=0; i letterCombinations(string digits) { 36 | if(digits.size()==0) return {}; 37 | return rec(digits); 38 | } 39 | }; -------------------------------------------------------------------------------- /Backtracking Session-1/diceRoll.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Write a recursive function diceRoll that accepts an integer 4 | representing a number of 6-sided dice to roll, and output all 5 | possible combinations of values that could appear on the dice. 6 | 7 | 8 | function diceRolls(dice): 9 | if dice == 0: 10 | nothing to do. 11 | else: 12 | //handle all roll values for a single die; let recursion do the rest. 13 | for each die value i in range [1..6]: 14 | choose that the current die will have value i. 15 | diceRolls(dice-1). // explore the remaining dice. 16 | un-choose the value i. 17 | 18 | */ 19 | 20 | #include 21 | using namespace std; 22 | 23 | void diceHelper(int dice, vector &chosen){ 24 | //base case 25 | if(dice == 0){ 26 | for(int i = 0; i < chosen.size(); i++) cout << chosen[i] << " "; 27 | cout << "\n"; 28 | return; 29 | } 30 | //d1 - 1,2,3,4,5,6 31 | for(int i = 1; i <= 6; i++){ 32 | chosen.push_back(i); 33 | diceHelper(dice-1,chosen); 34 | chosen.pop_back(); 35 | } 36 | } 37 | 38 | void diceRolls(int dice){ 39 | vector chosen; 40 | diceHelper(dice,chosen); 41 | } 42 | 43 | int main(){ 44 | diceRolls(3); 45 | } -------------------------------------------------------------------------------- /Recursion Session 2/getSubsequence.cpp: -------------------------------------------------------------------------------- 1 | //https://www.pepcoding.com/resources/online-java-foundation/recursion-with-arraylists/get-subsequence-official/ojquestion 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | vector gss(string s){ 8 | if(s.size()==0){ 9 | vector base; 10 | base.push_back(s); 11 | return base; 12 | } 13 | 14 | string smallString= s.substr(1); 15 | vector sans= gss(smallString); 16 | 17 | char ch=s[0]; 18 | vector ans; 19 | // not to add 20 | for(int i=0; i> s; 41 | vector ans = gss(s); 42 | int cnt = 0; 43 | 44 | cout << '['; 45 | for (string str : ans){ 46 | if (cnt != ans.size() - 1) 47 | cout << str << ", "; 48 | else 49 | cout << str; 50 | cnt++; 51 | } 52 | cout << ']'; 53 | } -------------------------------------------------------------------------------- /Backtracking Session-1/maze.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Consider a rat placed at (0, 0) in a square matrix of order N * N. 3 | It has to reach the destination at (N - 1, N - 1). 4 | Find all possible paths that the rat can take to reach from source to destination. 5 | The directions in which the rat can move are 6 | 'U'(up), 'D'(down), 'L' (left), 'R' (right). 7 | Value 0 at a cell in the matrix represents that it is blocked 8 | and rat cannot move to it while value 1 at a cell in the matrix represents 9 | that rat can be travel through it. 10 | 11 | Note: In a path, no cell can be visited more than one time. 12 | 13 | 1. Find what choice(s) we have at each step. 14 | What different options are there for the next step? 15 | 16 | 2. For each valid choice: 17 | a) Make it and explore recursively. 18 | Pass the information for a choice to the next recursive call(s). 19 | b) Undo it after exploring. 20 | Restore everything to the way it was before making this choice. 21 | 22 | 3. Find our base case(s). What should we do when we are out of decisions? 23 | 24 | */ 25 | 26 | #include 27 | using namespace std; 28 | 29 | void printPaths(vector>& maze){ 30 | int r = maze.size(); 31 | int c = maze[0].size(); 32 | } 33 | 34 | int main(){ 35 | vector> maze = {{1, 0, 0, 0}, 36 | {1, 1, 0, 1}, 37 | {1, 1, 0, 0}, 38 | {0, 1, 1, 1}}; 39 | 40 | printPaths(maze); 41 | } -------------------------------------------------------------------------------- /Backtracking Session-1/Subsets/subsets-1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Subsets are zero or more elements from a group of elements. 4 | 5 | S = {1, 2, 3} 6 | 7 | Output - {1, 2, 3}, {1, 2}, {1, 3}, {1}, {2, 3}, {2}, {3} , {} 8 | 9 | 1 2 3 10 | ---------- 11 | 1 2 3 12 | 1 2 13 | 1 3 14 | 1 15 | 2 3 16 | 2 17 | 3 18 | 19 | 20 | No. of subsets of n size array = 2^n 21 | How? 22 | Every element has 2 choices = 2*2*2*2... = 2^n 23 | 24 | For this consider all elements will be unique 25 | 26 | */ 27 | 28 | 29 | #include 30 | using namespace std; 31 | 32 | void printSubsetsHelper(vector &nums, vector &chosen, int startIdx){ 33 | // base case 34 | if(startIdx >= nums.size()){ 35 | for(auto &e : chosen) cout << e << " "; 36 | cout << "\n"; 37 | return; 38 | } 39 | 40 | int curr_elt = nums[startIdx]; 41 | 42 | //For current element, we have 2 choices 43 | 44 | //Choice 1: Keep curr elt in chosen/ans 45 | chosen.push_back(curr_elt); //[1] 46 | printSubsetsHelper(nums,chosen,startIdx+1); 47 | 48 | //Choice 2: Don't Keep curr elt in chosen/ans 49 | chosen.pop_back(); 50 | printSubsetsHelper(nums,chosen,startIdx+1); 51 | } 52 | 53 | 54 | void printSubsets(vector &nums){ 55 | vector chosen; 56 | printSubsetsHelper(nums,chosen,0); 57 | } 58 | 59 | int main(){ 60 | vector nums = {1,2,3,4}; 61 | printSubsets(nums); 62 | } 63 | 64 | 65 | /* 66 | 1,2,3 67 | 1,2 68 | 1,3 69 | 1 70 | 71 | 2,3 72 | 2 73 | 74 | 3 75 | 76 | 77 | */ 78 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": "(gdb) Attach", 9 | "type": "cppdbg", 10 | "request": "attach", 11 | "program": "${workspaceFolder}/a.exe", 12 | "processId": "${command:pickProcess}", 13 | "MIMode": "gdb", 14 | "miDebuggerPath": "c:\\mingw\\bin\\gdb.exe", 15 | "setupCommands": [ 16 | { 17 | "description": "Enable pretty-printing for gdb", 18 | "text": "-enable-pretty-printing", 19 | "ignoreFailures": true 20 | } 21 | ] 22 | }, 23 | { 24 | "name": "(gdb) Launch", 25 | "type": "cppdbg", 26 | "request": "launch", 27 | "program": "enter program name, for example ${workspaceFolder}/a.exe", 28 | "args": [], 29 | "stopAtEntry": false, 30 | "cwd": "${fileDirname}", 31 | "environment": [], 32 | "externalConsole": false, 33 | "MIMode": "gdb", 34 | "miDebuggerPath": "c:\\mingw\\bin\\gdb.exe", 35 | "setupCommands": [ 36 | { 37 | "description": "Enable pretty-printing for gdb", 38 | "text": "-enable-pretty-printing", 39 | "ignoreFailures": true 40 | } 41 | ] 42 | } 43 | ] 44 | } -------------------------------------------------------------------------------- /Recursion Assignment.md: -------------------------------------------------------------------------------- 1 | Done in the live session-1: 2 | 3 | 1. https://practice.geeksforgeeks.org/problems/print-1-to-n-without-using-loops-1587115620/1 4 | 2. https://practice.geeksforgeeks.org/problems/factorial5739/1 5 | 3. https://www.geeksforgeeks.org/program-count-digits-integer-3-different-methods/ 6 | 4. https://leetcode.com/problems/power-of-two/ 7 | 5. https://leetcode.com/problems/fibonacci-number/ 8 | 6. https://leetcode.com/problems/reverse-string/ 9 | 7. https://practice.geeksforgeeks.org/problems/consecutive-elements2306/1 10 | 8. https://practice.geeksforgeeks.org/problems/consecutive-1s-not-allowed1912/1 11 | 9. https://leetcode.com/problems/climbing-stairs/ 12 | 13 | Take Home Assignment: 14 | 15 | Easy : 16 | 17 | 1. https://practice.geeksforgeeks.org/problems/juggler-sequence3930/1/ 18 | 2. https://practice.geeksforgeeks.org/problems/print-all-possible-strings/1/ 19 | 3. https://www.pepcoding.com/resources/online-java-foundation/recursion-in-arrays/all-indices-official/ojquestion 20 | 4. https://practice.geeksforgeeks.org/problems/gf-series3535/1/ 21 | 5. https://practice.geeksforgeeks.org/problems/recursive-sequence1611/1 22 | 6. https://practice.geeksforgeeks.org/problems/print-pattern3549/1/ 23 | 7. https://practice.geeksforgeeks.org/problems/permutations-of-a-given-string2041/1 24 | 8. https://leetcode.com/problems/powx-n/ 25 | 26 | For tomorrow's live sessions: 27 | 28 | 1. https://practice.geeksforgeeks.org/problems/tower-of-hanoi-1587115621/1/ 29 | 2. https://practice.geeksforgeeks.org/problems/josephus-problem/1 30 | 3. https://www.pepcoding.com/resources/online-java-foundation/recursion-with-arraylists/get-subsequence-official/ojquestion 31 | 4. https://practice.geeksforgeeks.org/problems/subset-sums2234/1 32 | 5. https://practice.geeksforgeeks.org/problems/print-n-bit-binary-numbers-having-more-1s-than-0s0252/1/ 33 | 6. https://practice.geeksforgeeks.org/problems/next-happy-number4538/1/ 34 | 35 | Some More Questions: 36 | 37 | 1. https://leetcode.com/problems/product-of-array-except-self/ 38 | 2. https://practice.geeksforgeeks.org/problems/quick-sort/1 39 | 3. https://practice.geeksforgeeks.org/problems/merge-sort/1 40 | 4. https://practice.geeksforgeeks.org/problems/binary-search/1 41 | 5. https://practice.geeksforgeeks.org/problems/triplet-sum-in-array-1587115621/1 - Try with recursion and see what time complexity you get 42 | 43 | Try these yourself and ask your doubts or discuss your approaches in the upcoming Doubt Session 44 | -------------------------------------------------------------------------------- /Assignment.md: -------------------------------------------------------------------------------- 1 | Done in the live session-1: 2 | 3 | 1. https://practice.geeksforgeeks.org/problems/print-1-to-n-without-using-loops-1587115620/1 4 | 2. https://practice.geeksforgeeks.org/problems/factorial5739/1 5 | 3. https://www.geeksforgeeks.org/program-count-digits-integer-3-different-methods/ 6 | 4. https://leetcode.com/problems/power-of-two/ 7 | 5. https://leetcode.com/problems/fibonacci-number/ 8 | 6. https://leetcode.com/problems/reverse-string/ 9 | 7. https://practice.geeksforgeeks.org/problems/consecutive-elements2306/1 10 | 8. https://practice.geeksforgeeks.org/problems/consecutive-1s-not-allowed1912/1 11 | 9. https://leetcode.com/problems/climbing-stairs/ 12 | 13 | Take Home Assignment: 14 | 15 | Easy : 16 | 17 | 1. https://practice.geeksforgeeks.org/problems/juggler-sequence3930/1/ 18 | 2. https://practice.geeksforgeeks.org/problems/print-all-possible-strings/1/ 19 | 3. https://www.pepcoding.com/resources/online-java-foundation/recursion-in-arrays/all-indices-official/ojquestion 20 | 4. https://practice.geeksforgeeks.org/problems/gf-series3535/1/ 21 | 5. https://practice.geeksforgeeks.org/problems/recursive-sequence1611/1 22 | 6. https://practice.geeksforgeeks.org/problems/print-pattern3549/1/ 23 | 7. https://practice.geeksforgeeks.org/problems/permutations-of-a-given-string2041/1 24 | 8. https://leetcode.com/problems/powx-n/ 25 | 26 | For tomorrow's live sessions: 27 | 28 | 1. https://practice.geeksforgeeks.org/problems/tower-of-hanoi-1587115621/1/ 29 | 2. https://practice.geeksforgeeks.org/problems/josephus-problem/1 30 | 3. https://www.pepcoding.com/resources/online-java-foundation/recursion-with-arraylists/get-subsequence-official/ojquestion 31 | 4. https://practice.geeksforgeeks.org/problems/subset-sums2234/1 32 | 5. https://practice.geeksforgeeks.org/problems/print-n-bit-binary-numbers-having-more-1s-than-0s0252/1/ 33 | 6. https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 34 | 7. https://practice.geeksforgeeks.org/problems/next-happy-number4538/1/ 35 | 36 | Some More Questions: 37 | 38 | 1. https://leetcode.com/problems/product-of-array-except-self/ 39 | 2. https://practice.geeksforgeeks.org/problems/quick-sort/1 40 | 3. https://practice.geeksforgeeks.org/problems/merge-sort/1 41 | 4. https://practice.geeksforgeeks.org/problems/binary-search/1 42 | 5. https://practice.geeksforgeeks.org/problems/triplet-sum-in-array-1587115621/1 - Try with recursion and see what time complexity you get 43 | 44 | Try these yourself and ask your doubts or discuss your approaches in the upcoming Doubt Session 45 | -------------------------------------------------------------------------------- /Recursion Session 1/After creating Mantra.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | int countOfDigits(int n) { 6 | // base case 7 | if(n==0) return 0; 8 | 9 | // recursion work 10 | int small_ans = countOfDigits(n/10); 11 | 12 | // my-work 13 | int ans = 1+small_ans; 14 | 15 | return ans; 16 | 17 | } 18 | 19 | int sumOfDigits(int n) { 20 | // base case 21 | if(n==0) return 0; 22 | 23 | // recursion work 24 | int small_ans = sumOfDigits(n/10); 25 | 26 | // my-work 27 | int ans = n%10 + small_ans; 28 | 29 | return ans; 30 | } 31 | 32 | // 0 1 1 2 3 5 8 13 21... 33 | int nthFibonacci(int n) { 34 | // base case 35 | if(n == 0 || n == 1) return n; 36 | 37 | // recursion work 38 | int prev_fib = nthFibonacci(n-2); 39 | int prevprev_fib = nthFibonacci(n-1); 40 | 41 | // my-work 42 | int ans = prev_fib + prevprev_fib; 43 | 44 | return ans; 45 | } 46 | /* 47 | 48 | f(n) = f(n-1) + f(n-2) 49 | 50 | tn = tn-1 + tn-2 , tn-1~tn-2 51 | tn = 2*tn-1 52 | , tn-1 = tn-2+tn-3 = 2*tn-2 53 | tn = 2*2*tn-2 54 | , tn-2 = tn-3+tn-4 = 2*tn-3 55 | tn = 2*2*2*tn-3 56 | 57 | n-k = 0 58 | n = k 59 | tn = 2^k*tn-k 60 | tn = 2^n 61 | 62 | */ 63 | 64 | 65 | //D E SI R E 66 | //e + rised //o(n) 67 | 68 | //TC = no. of recursive calls * time taken in 1 call 69 | 70 | void reverseString(string &s, int lo, int hi){ 71 | // base case 72 | if(lo >= hi) return; 73 | 74 | // recursion work 75 | reverseString(s,lo+1,hi-1); 76 | 77 | // my-work 78 | swap(s[lo],s[hi]); 79 | } 80 | 81 | void reverseString(string &s) { 82 | reverseString(s,0,s.size()-1); 83 | } 84 | 85 | //NITIN = NITIN 86 | //DESIRE != DESIRE 87 | //MOMO != OMOM 88 | bool isPalindrome(string &s, int lo, int hi){ 89 | if(lo >= hi) return 1; 90 | 91 | if(s[lo] == s[hi] && isPalindrome(s,lo+1,hi-1)) return 1; 92 | 93 | return 0; 94 | } 95 | 96 | bool isPalindrome(string &s){ 97 | return isPalindrome(s,0,s.size()-1); 98 | } 99 | 100 | //aabbdddef = abdef 101 | // TC = n*n 102 | string removeDuplicates(string s) { 103 | // base case 104 | if(s.size() <= 1) return s; 105 | 106 | // recursion work 107 | string small_ans = removeDuplicates(s.substr(1)); 108 | 109 | // my-work 110 | if(s[0] == small_ans[0]) return small_ans; 111 | 112 | return s[0] + small_ans; 113 | } 114 | 115 | int nonConsecutiveOnes(int n){ 116 | // base case 117 | if(n==1) return 2; 118 | if(n==2) return 3; 119 | 120 | // recursion work 121 | return nonConsecutiveOnes(n-1)+nonConsecutiveOnes(n-2); 122 | } 123 | 124 | int StairCase(int n){ 125 | // base case 126 | if(n < 0) return 0; 127 | if(n==0) return 1; 128 | // recursion work 129 | return StairCase(n-1) + StairCase(n-2); 130 | 131 | // my-work 132 | return 0; 133 | } 134 | 135 | bool isPowerOfTwo(int n) { 136 | // base case 137 | 138 | // recursion work 139 | return 0; 140 | // my-work 141 | } 142 | 143 | int main() { 144 | cout << "countOfDigits: " << countOfDigits(54162); 145 | cout << "\n\nsumOfDigits: " << sumOfDigits(54162); 146 | cout << "\n\nnth Fibonacci: " << nthFibonacci(6); 147 | 148 | string s = "DESIRE"; 149 | cout << "\n\nOriginal String: " << s; 150 | reverseString(s); 151 | cout << "\n\nreversedString: " << s; 152 | string s2 = "NITIN"; 153 | cout << "\n\nisPalindrome: " << s << " " << isPalindrome(s); 154 | cout << "\n\nisPalindrome: " << s2 << " " << isPalindrome(s2); 155 | // printSubsets("abc"); 156 | cout << "\n\nremoveDuplicates: " << removeDuplicates("abcddcba"); 157 | // cout << "\n\ncountNonConsecutiveOnes" << countNonConsecutiveOnes(5); 158 | // cout << "\n\nstairCase" << stairCase(5); 159 | 160 | return 0; 161 | } -------------------------------------------------------------------------------- /Recursion Session 2/AssignmentSolutions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // 1============================================== 5 | class Solution{ 6 | public: 7 | void juggler(vector& ans, long long N){ 8 | ans.push_back(N); 9 | 10 | if(N==1){ 11 | return; 12 | } 13 | 14 | if(N%2==0){ 15 | juggler(ans,sqrt(N)); 16 | } else { 17 | juggler(ans,sqrt(N*N*N)); 18 | } 19 | } 20 | vector jugglerSequence(int N){ 21 | vector ans; 22 | juggler(ans,(long long)N); 23 | return ans; 24 | } 25 | }; 26 | 27 | // 2 ======================================= 28 | void rec(string input,int index,string output,vector &v){ 29 | if(index==input.size()){ 30 | v.push_back(output); 31 | return; 32 | } 33 | 34 | rec(input,index+1,output+input[index],v); 35 | 36 | rec(input,index+1,output+" "+ input[index],v); 37 | 38 | 39 | } 40 | 41 | vector spaceString(char str[]) 42 | { 43 | string input = str; 44 | vectorv; 45 | string curr=""; 46 | curr+=input[0]; 47 | rec(input,1,curr,v); 48 | return v; 49 | } 50 | 51 | 52 | // 3 ========================================================================== 53 | vector allIndex(vector& arr, int i, int tar) 54 | { 55 | if(i==arr.size()){ 56 | vector base; 57 | return base; 58 | } 59 | vector ans; 60 | 61 | 62 | vector sans=allIndex(arr,i+1,tar); 63 | 64 | if(arr[i]==tar){ 65 | ans.push_back(i); 66 | } 67 | for(int i=0; in)return 0; 109 | 110 | return solve(i,cnt)+recurseq(n,i+1,cnt+i); 111 | } 112 | 113 | long long sequence(int n){ 114 | return recurseq(n, 1, 1); 115 | } 116 | }; 117 | 118 | // 6=========================================================================================== 119 | class Solution{ 120 | public: 121 | void Solve(int n,vector& v){ 122 | v.push_back(n); 123 | if(n<=0){ 124 | return; 125 | } 126 | 127 | Solve(n-5,v); 128 | 129 | v.push_back(n); 130 | } 131 | 132 | vector pattern(int N){ 133 | vector v; 134 | 135 | Solve(N,v); 136 | 137 | return v; 138 | } 139 | }; 140 | 141 | // 7========================================================================================= 142 | 143 | class Solution { 144 | public void rec(String s,String curr,List ans){ 145 | if(s.length()==0){ 146 | ans.add(curr); 147 | return; 148 | } 149 | for(int i=0; i find_permutation(String S) { 157 | List ans=new ArrayList<>(); 158 | rec(S,"",ans); 159 | Collections.sort(ans); 160 | return ans; 161 | } 162 | } 163 | // 8========================================================================================= 164 | 165 | class Solution { 166 | public: 167 | double myPow(double x, int n) { 168 | if(n==0) return 1; 169 | 170 | double y = myPow(x,n/2); 171 | if(n%2==0){ 172 | 173 | return y*y; 174 | 175 | } 176 | else{ 177 | return n < 0 ? 1/x*y*y : x*y*y; 178 | } 179 | } 180 | }; 181 | --------------------------------------------------------------------------------