# Product Sum

Master the calculation of product sums for nested lists with this essential algorithm.

Recursion
easy

Published At

6/4/2021

Write a function that takes in a "special" array and returns its product sum.

A "special" array is a non-empty array that contains either integers or other "special" arrays. The product sum of a "special" array is the sum of its elements, where "special" arrays inside it are summed themselves and then multiplied by their level of depth.

The depth of a "special" array is how far nested it is. For instance, the depth ofÂ []Â isÂ 1; the depth of the inner array inÂ [[]]Â isÂ 2; the depth of the innermost array inÂ [[[]]]Â isÂ 3.

Therefore, the product sum ofÂ [x, y]Â isÂ x + y; the product sum ofÂ [x, [y, z]]Â isÂ x + 2 * (y + z); the product sum ofÂ [x, [y, [z]]]Â isÂ x + 2 * (y + 3z).

### Sample Input

js
``array = [5, 2, [7, -1], 3, [6, [-13, 8], 4]]``
js
``array = [5, 2, [7, -1], 3, [6, [-13, 8], 4]]``

### Sample Output

js
``12 // calculated as: 5 + 2 + 2 * (7 - 1) + 3 + 2 * (6 + 3 * (-13 + 8) + 4)``
js
``12 // calculated as: 5 + 2 + 2 * (7 - 1) + 3 + 2 * (6 + 3 * (-13 + 8) + 4)``

### Hints

Hint 1

Try using recursion to solve this problem.

Hint 2

Initialize the product sum of the "special" array to 0. Then, iterate through all of the array's elements; if you come across a number, add it to the product sum; if you come across another "special" array, recursively call the productSum function on it and add the returned value to the product sum. How will you handle multiplying the product sums at a given level of depth?

Hint 3

Have the productSum function take in a second parameter: the multiplier, initialized to 1. Whenever you recursively call the productSum function, pass in the multiplier incremented by 1.

Optimal Space & Time Complexity

O(n) time | O(d) space - where n is the total number of elements in the array, including sub-elements, and d is the greatest depth of "special" arrays in the array

Solution-1
js
``````// Tip: You can use the Array.isArray function to check whether an item
// is a list or an integer.
function productSum(array) {
return sumDepth(array, 1)
}

function sumDepth(array, depth) {
let sum = 0;
for (let i = 0; i < array.length; i++) {
if (Number.isInteger(array[i])) {
sum += array[i];
} else {
sum += sumDepth(array[i], depth + 1)
}
}
return depth * sum;
}``````
Solution-1
js
``````// Tip: You can use the Array.isArray function to check whether an item
// is a list or an integer.
function productSum(array) {
return sumDepth(array, 1)
}

function sumDepth(array, depth) {
let sum = 0;
for (let i = 0; i < array.length; i++) {
if (Number.isInteger(array[i])) {
sum += array[i];
} else {
sum += sumDepth(array[i], depth + 1)
}
}
return depth * sum;
}``````

đŠ«

Do you have any questions, or simply wish to contact me privately? Don't hesitate to shoot me a DM on Twitter.

Have a wonderful day.
Abhishek đ

## Join My Exclusive Newsletter Community

Step into a world where creativity intersects with technology. By subscribing, you'll get a front-row seat to my latest musings, full-stack development resources, and exclusive previews of future posts. Each email is a crafted experience that includes:

• In-depth looks at my covert projects and musings to ignite your imagination.
• Handpicked frontend development resources and current explorations, aimed at expanding your developer toolkit.
• A monthly infusion of inspiration with my personal selection of quotes, books, and music.

Embrace the confluence of words and wonder, curated thoughtfully and sent straight to your inbox.

No fluff. Just the highest caliber of ideas.