How to good at recursion?

Most of time can 't abe to figure out how to use recursive approch when getting idea that this question going to solve by recursion.???


First of all, let me say that I’m learning myself, but I can share with you some tips.
Here’s how I started. Solve all the problems from Recursion 1 and Recursion 2 given on CodingBat. You have to write Java functions. I see that you mentioned C++ as a tag, but if you’re comfortable with C++, writing a Java function is very similar. Here’s the link to the Recursion 1.

Once you’ve got all these under your belt, study some recursive solutions from Geeksforgeeks under the DP section.
Remember, if you start plugging in values in recursive functions, you’ll be stuck almost all the time. Just pick a point during the execution and try to see how you can reach that point from other lower states.
Good luck. =]


The best way is to start with a good and fresh article about recursion, read it. Then read the newest article that is cited as reference of this and then read the newest article cited as reference of this second article and so on … Since you don’t want to read forever, you need to define a condition to stop reading, for example if the article’s date of publication is before 1950 you stop. Belive me, once you’ve done that, I’m pretty sure you’ve become really good at recursion


Im too facing same problem not able to get recursive approach…i know alternative is iterative method but then also,and sometimes in recursive solution of problem it is difficult to track manually what is happening so can somebody explain taking an example of any recursion problem and show how to think of the problem
like this one

// subsequences for given array using 
// recursion 
#include <bits/stdc++.h> 
using namespace std; 
void printArray(vector<int> arr, int n) 
    for (int i = 0; i < n; i++) 
        cout << arr[i] << " "; 
    cout << endl; 
// Recursive function to print all 
// possible subsequences for given array 
void printSubsequences(vector<int> arr, int index,  
                       vector<int> subarr) 
    // Print the subsequence when reach 
    // the leaf of recursion tree 
    if (index == arr.size()) 
        int l = subarr.size(); 
        // Condition to avoid printing 
        // empty subsequence 
        if (l != 0) 
            printArray(subarr, l); 
        // Subsequence without including 
        // the element at current index 
        printSubsequences(arr, index + 1, subarr); 
        // Subsequence including the element 
        // at current index 
        printSubsequences(arr, index + 1, subarr); 
// Driver Code 
int main() 
    vector<int> arr{1, 2, 3}; 
    vector<int> b; 
    printSubsequences(arr, 0, b); 
    return 0; 
1 Like

@aryanc403 @sebastian

Is there similar site like codingbat where we have option for submitting it in C++.??

The best way to solve a Recursion based question is to use think smaller versions of the problems and try to use the solution of these smaller problems to get the answers of the bigger problems.
Now the second step for solving these problems is to think of the base cases when you call no more and return a value, perhaps you see a integer has reached its smallest possible value and you return a value for this case without doing computation.
The third and final stage is to call the function and its where it gets tricky how do you decide how to call the function?
Remember I said of Thinking for smaller cases ? Think of them and try to formulate how they are related and try to write the correct expression…
for example you see fun(3)=fun(2)+fun(1)
so it comes naturally to you that the fun(n)=fun(n-1)+fun(n-2).
The best thing about recursion is that it can solve complex looking problems by writing a very small code