Only two cases got passed

Hello @Pranav_Chaudhary,

  1. No need to swap back

  2. When you use char array, then the changes made in it are reflected back to the block that has called the function.
    i.e. changes in formal parameters are reflected back in the actual parameter.
    Solution:
    2.1.You can use string as it does not reflect back the changes

    2.2. If you would use an array, then you have to return the char array in that you accepted as input to the array without imposing any changes that are done in the current functional block. Then, this returned array is assigned to an array of calling functional block.

I have modified your code, let me know if you don’t understand this.

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

Hi Virender,

I am not able to understand the solution given by you

Hey @Pranav_Chaudhary,

I have modified your code only.
I have changed the return type to char array.

Why?
Let’s Take an example(Pseudo code) to understand what is happening in this program.

Suppose, you have three functions f1, f2 and f3 as:

void f1( char [] arr1,int n){
//Changes in arr1
}

//Calling f1
void f2( char [] arr2,int n){
f1(arr,n)
//Changes in arr2
}

//Calling f2
void f3( char [] arr,int n){
f2(arr,n)
//Changes in arr
}

What is happening, when you make changes in the arr1 inside f1(called function) then the array present inside f2(calling), aar2 also changes.
This is happening because array is passed by reference implicitly.

But i want that whatever changes i make changes in the array(parameter passed) inside the called function should not be reflected in the array present in the calling function.

So, to achieve above, i am making a copy of array that is passed as parameter and then return the same array.
Why am i returning the array?
So, that i can the returned array to the array present inside the calling function so that it can retain its previous values before the function call.

IMPORTANT:
You can do one more thing if you are still not getting this approach.
Instead of passing arr in the function call. Pass its copy.
So, all the changes will be made to its copy, not to the array:

Refer the following code, it is comparatively simpler:

Let me know, if you still face issue.

HI virender,
Thanks for explaining. It swaps like upper 3 calls would be
1.abc and work on bc part
2. bac and work on ac part
and final would be cab and work on ab part.

And your are copying before calling for subpart.

Still my question is I am swapping again to maintain its original arrray using backtracking as parteek bhaia explain in his video.
Would you please give me solution using backtracking rather than copying the array so that i can relate.

But i got your point to copy the array.

Hey @Pranav_Chaudhary,

That’s the point.
I did all the extra copying thing because the order in which the question demand the output or strings to be printed is not exactly how dictionary stores word.

Its solution is not possible using backtracking.
If you’ll use backtracking then the output that your code would produce would be different than the required one(in terms of order).

And I was expecting that question:
I am swapping again to maintain its original array

There is a difference between what the second swapping statement does and what I have told:
if you observe the flow of both the approach carefully:

for (int j = i; j < in.length; j++) {
swap(in, i, j);
//Create copy of array in
char[] c_in = Arrays.copyOf(in, in.length);
permute(origninal, c_in, i + 1);
//No need to swap back
// swap(in, i, j);
}

  1. if you’ll do swapping then you backtrack to original position before the next swap but in the same function call.
  2. In my code, changes persist within the called function for each swap but do not reflects back in the called function.

Can you see the difference?

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.