Hi everyone.
Let's do this question.
Let's define a peculiar type of array in which each element is either an integer or another peculiar
array.
All right.
So over here this indicates that you have a peculiar type of array.
And each element is either an integer or again another peculiar array.
So notice that this indicates recursion right.
So this is a hint that we are dealing with something that can be solved with recursion.
Now let's proceed.
Assume that a peculiar array is never empty.
Write a function that will take a peculiar array as its input and find the sum of its elements.
All right, so we have to just take in that array and find the sum.
If an array is an element.
In the peculiar array, you have to convert it to its equivalent value.
All right.
So if the element is an array then you have to convert it to its equivalent value so that you can sum
it with the other elements.
All right.
So otherwise you won't be able to sum an integer and an array.
Now equivalent value of an array is the sum of its elements raised to the number which represents how
far nested it is.
So you have to raise it right.
Raise to a power.
So it's an exponent.
For example two three.
And then you have this element over here which is an array 412.
Now for this to find the sum you have to two plus three.
And then you have to convert this to its equivalent value.
And over here it said that's four plus one plus two to the power two.
Right.
So it is raised to the number which represents how far nested it is.
So this is nested in to level two right.
So that's why we have to the power two over here.
Now in this case we have one two.
So we are adding these two.
And then over here we have seven.
And then we have this element over here.
And we have two.
So seven plus the equivalent value of this element plus two to the power two because this is the second
level right.
And for the equivalent value of this one over here notice we have three plus four to the power three
because this is the third level.
Now you can also say that what this is doing over here is that if you have let's say an array with just
three elements one, two, three, what we're doing is one plus two plus three to the power one because
this is of level one right.
So we have these elements.
So I hope you have understood the question.
Let's again summarize what we have understood.
So we will be given a non-empty peculiar array.
And then we have to find the sum of that peculiar array.
And the sum is the sum of the elements.
Now if the element of the array is an is an array itself, then you have to convert.
For example, over here you have two which is an element which is a three.
This is an integer.
So yes two and three can be added.
But then the next element over here is 412.
So there should be a comma over here.
So that's 412.
And if that is the case then this has to be converted to its equivalent number.
Only then it can be added to two plus three.
Right.
And for this we have to find its sum which is four plus one plus two.
And it has to be raised to the power where the level is, where it's raised, to the number which represents
how far nested it is.
So this is the this is two levels deep right.
This is level one and this is level two.
So that's why we are raising to the power two.
Now in this example this was three levels deep.
This is level one.
This is level two.
And then this is level three.
So over here we have three plus four to the power three.
And then we add that to seven and two and raise it to the power two.
So we get the equivalent value of this element over here.
And that is added to one and two.
All right.
So this is the question over here.
And again notice over here we saw something interesting in the question.
When we read it we it's mentioned over here we have a particular type of array.
And each element is either an integer or another peculiar array.
So this indicates recursion right.
So we have something and something inside that can also be that something.
So over here this indicates recursion.
So these type of problems typically can be solved with recursion.
All right.
So we have read the question.
We have understood it.
Now let's proceed and write a few test cases so that we get a good understanding of the question.
And then we will look at how we can solve this question.
Again we have a few test cases over here.
Let's again write a few more so that we get familiarized with the question.
All right.
Now let's take a very simple case.
Let's say the array which is given to us is one, two, three.
So in this case we just need to add these.
All right.
One plus two plus three which is equal to six.
Right.
And again over here the level is one.
So we can say that we are raising this to the power one which does not change.
Six.
Right.
The answer is still six.
Now let's say we have this array over here one two.
And then this element is not an integer.
It's an it's another array.
Right.
So in this case we have to convert this to its equivalent value so that we can add it to these two.
So the answer over here will be one plus two plus three to the power two.
Because this is level one.
This bracket is level one and this bracket is level two.
Right.
So we have to do three to the power two.
And this is nine nine plus two is 11 plus one is 12.
So this gives us the answer is 12.
And again in the question it's mentioned that the input will be non-empty.
So we don't need to consider that case right now.
Let's say the input is this one over here it's a little bit more complicated.
So we have one we have two.
So basically we need to convert this array into its equivalent value which is three plus four to the
power of this level of the brackets.
Right.
So this is level one.
This one over here is level one.
Level one.
This is level two right.
This also is level two right.
And this is within level two.
So this is level three.
So over here this can be converted to three plus four which is seven to the power two.
So that is seven square right.
Now what about this value over here.
This two is in level three right.
So we have to do two cube.
So we get eight.
Now we have still level two over here right.
These brackets.
So we have to do eight to the power two which gives us 64.
So we have everything now in level one.
So we just need to add these up.
127 square is 49 and eight square is 64.
So let's add them one plus two plus 49 plus 64.
And again we could raise this to power one which is the same thing right.
But it does not change the answer.
And this is equal to 116.
And this is what is expected from the function that we have to write.
So we have got a good understanding of the question.
Now let's proceed and think through the logic or the method which we can use to solve this problem.