Recursion-Subsequences error is coming

how to remove the extra part in recursion plz tell how to remove and modify my code

here is the link-

Hello @ayush1213,

There are two issues with in your code:

  1. You are not displaying the count of subarrays, which should be the second line of output.
  2. The subarrays your code is producing are different from what it should actually print.

Let’s tackle both of them one by one.

  1. For the count, use a variable initialized to 0 and is incremented every time a substring is formed.
    (inside the if condition, where you are printing substrings)

  2. This is because you have not handled the recurssion properly.
    I would suggest you to use string container over character array as it supports many functions which makes it easy to handle.
    Refer to the following code for better understanding:
    https://ide.codingblocks.com/s/114131

Hope, this would help.
Give a like, if you are satisfied.

1 Like

@S18ML0016 sir i tackled first problem in my code sir but i want to know what error(2) i m doing in my code sir i also didn’t understand the how u pass the ans+ch what is this thing doing

Sure,

If you have to understand the difference between both the logics :
The best way is to dry both the codes one by one for a small input like: abc
If you still face any issue, let me know.

Now, coming back to the code that I have shared:
Let’s dry run it for abc to understand it’s flow:

  1. for first pass:
    q=abc and ans=""
    ch=q[0]=a
    ros=bc

  2. for second call: printsubseq(ros, ans);
    q=bc and ans=""
    ch=q[0]=b
    ros=c

  3. for third pass: printsubseq(ros, ans);
    q=c and ans=""
    ch=q[0]=c
    ros=""

  4. for fourth pass: printsubseq(ros, ans);
    q="" and ans=""
    Print ans i.e. “”

    Now, as we have reached the base case, so, we will return.
    There are two purpose of using the statement: printsubseq(ros, ans);
    4.1. Print the empty string
    4.2. To iterate to the last of the string

  5. for fifth pass: printsubseq(ros, ans + ch);
    Now, we starting printing the substring from right, as required by the question.
    q="" and ans=ans+ch=""+c=c
    Print ans i.e. c
    Return back

Next iteration will print , c
Then cd,
Similarly, the rest of the substring would be printed.

Hope, this would help.

1 Like

thanks sir i got it very well thank you for your efforts

Anytime. :blush:
Please, mark this doubt as resolved if you have no more questions to ask regarding this question.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.