Welcome back.
Let's now code the solution which we discussed in the previous video.
Now for this we are going to write a function and let's just call it power set.
And this function is going to take in the array which is given to us.
So let's call the array numb's.
All right, now over here inside this we are going to have the output array.
So let's just call it output itself.
And you're going to have a helper function, which we will recursively call, and at the end we will
just return this output.
So this is what we're going to do.
Now.
Let's go ahead and write the helper function.
Now let's call it helper itself.
So const helper is equal to function.
And this function is going to take in the array.
And it is going to take in the index I and a subset.
Right.
So the subset which we are working with.
And then inside this helper function we are going to check whether I is equal to the length of the given
array.
So if I is equal to numb's dot length.
If that is the case, then we will push the subset.
That is what we are working with.
We will make a copy of the subset and we will push it to the output array.
Right.
So we will say output dot push.
And then we'll say subset.
Dot slice.
Right.
So this is where we are using dot slice over here so that we are making a copy of the subset array at
that point.
And we are pushing it to the output array.
All right.
And after we are done with this we can just return.
So return.
All right.
Now if this is not the case then we will have two cases right.
We will have one case where we don't add the element at the ith index.
And we will have a case where we add the element right.
So we will have don't add and add.
Two cases are there.
Now first let's call the helper function where we don't add the i'th element.
So we say helper.
That's a recursive call.
And again we pass the array numb's.
And at this point we increment I.
So we say I plus one.
And over here we have subset.
Which is what was the subset at when when we are over here.
Right.
So we don't change the subset we pass the same subset.
That is we don't add anything to the subset and we just increment I.
Now the other case is where we push to the subset the element at the ith index.
So we can say subset dot push.
Numb's at I.
So we push the element at the ith index to the subset.
And then we call the helper function.
So helper numb's we pass numb's.
And in this case also we increment I.
So I plus one.
And we pass the subset now so that we can use the same subset.
After we have done with this we will just pop from subset what we just pushed to it.
So we say subset dot pop.
And we are done with the help of function.
Now all we need to do is we need to call the helper function the first time.
So we will do that over here.
So we say helper.
And at this point we are passing the array.
And I at this point the index is going to be zero.
And the subset is going to be an empty array.
And that's it.
Now let's call the function and let's see whether it's working.
So the function is power set.
And let's call it for an array.
Let's say one two and three.
All right.
So let's just call it.
Now let's look at the output.
We can see that we have eight elements which is correct.
We have seen that the power set should have two to the power n elements.
And n in this case is three.
So there should be two to the power 3 or 8 elements which is correct.
Now let's look at the elements.
We have the empty set empty array.
Then we have three arrays with one element only so one, two and three.
Then we have one and three, one and two which is correct.
We have two and three and we have one, two and three.
So yes that's correct.
So we have a total of eight elements.
So our function is working.
Now what if we pass in an empty array.
So we should have just one element right.
So there's the the output array should have just one element.
And that should be an array with zero elements.
So yes that's correct.
Now what if we have one element over here.
Let's try that case.
All right.
In this case we have two to the power one which is two elements over here.
That's correct.
So this is empty.
And this has just one element which is nine.
So yes our function is working.
Now let's take a sample input and walk through the code once more.
And also take a relook at the space and time complexity of the solution.