Nested Array Sum

Recursion Nested Arrays Algorithm

Problem: Nested Array Sum

Given an array that can contain both integers and other nested arrays of integers, write a recursive function that returns the sum of all integers in the nested array structure.

For example, given the input:

[1, [2, [3, 4], 5], 6]

Your function should return 21 (i.e., 1 + 2 + 3 + 4 + 5 + 6).

Note: February 2 is Groundhog Day, a day when events tend to repeat. In a similar way, nested arrays may have elements that repeat the pattern of containing more arrays. Use recursion to flatten these repetitions and compute the total sum.

Requirements:

  • Use a recursive approach to handle arbitrarily nested arrays.
  • Assume the input contains only integers and nested arrays.
  • Your solution should be language-agnostic and implementable in any programming language.

Happy coding!