## Product Sum 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

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

### Sample Output

`112 // 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

```1// Tip: You can use the Array.isArray function to check whether an item2// is a list or an integer.3function productSum(array) {4  return sumDepth(array, 1)5}6
7function sumDepth(array, depth) {8    let sum = 0;9    for (let i = 0; i < array.length; i++) {10        if (Number.isInteger(array[i])) {11            sum += array[i];12        } else {13            sum += sumDepth(array[i], depth + 1)14        }15    }16    return depth * sum;17}```

🦫

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 🙏
{"ft_lang_label":"__label__en","ft_lang_prob":0.7313155,"math_prob":0.98880905,"size":2101,"snap":"2023-40-2023-50","text_gpt3_token_len":593,"char_repetition_ratio":0.18025751,"word_repetition_ratio":0.028277636,"special_character_ratio":0.31080437,"punctuation_ratio":0.12727273,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9966629,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-10-04T07:11:46Z\",\"WARC-Record-ID\":\"<urn:uuid:a64ef59d-9360-4191-bc34-d3364bbca04a>\",\"Content-Length\":\"163864\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:07a4e4aa-366c-4806-a75a-49c534ab67e7>\",\"WARC-Concurrent-To\":\"<urn:uuid:ab209640-02fb-40db-98ff-1ca8c0fc5013>\",\"WARC-IP-Address\":\"76.76.21.21\",\"WARC-Target-URI\":\"https://abheist.com/15-product-sum/\",\"WARC-Payload-Digest\":\"sha1:XKYK2T7PVUYNEMKNJVLAS7VJEV364DF7\",\"WARC-Block-Digest\":\"sha1:7ZAMSNJ74A6BHMBEGJCA2B5HVSNNXXKM\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233511361.38_warc_CC-MAIN-20231004052258-20231004082258-00355.warc.gz\"}"}