Move x at the end of string

if i don’t write return in line no 12,
i face run timr error while it is okay in line no 19

so basically i want to ask that when to use return and when not to use

Hi @ynikhil1999, in void functions, in case of recusrion, use return only when you want to end a particular recursive call. Like you did in line 9 in your code.
Suggestion, try to solve it using an approach invoolving 2 pointers, because here when j becomes n-1, you are again repeating steps and hence time complexity of your soln becomes O(n*n). Try to solve it on O(n).

Hope this helps :slight_smile:

bro…can u send the pseudo code for 2 pointer approach

l = 0, r = n-1

// r may or may not be at n-1. Think about this after you have made and executed code once.

while ( < think abt the cond urself > ) {
        if ( s[l]==‘x’ )
                swap(s[l],s[r]) ;
                l++; r-- ;
        else
                l++ ;
}

Here’s the pseudocode, its missing some conditions and base work. Your job is to think how can you apply the above logic in solving the given problem. Also as I said earlier, the base tasks and conditions are missing, you have to decide them on your own, and they will be solely based on how use the above pseudocode.

Hope this helps :slight_smile:

1 Like

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.