JS solution with example
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```javascript []
/**
 * @param {Array} arr
 * @param {number} n
 * @return {Array}
 */
var flat = function (arr, n) {
    const result = [];
    
    const helper = (array, depth) => {
        for (const item of array) {
            if (Array.isArray(item) && depth < n) {
                helper(item, depth + 1); // Recursively flatten if depth < n
            } else {
                result.push(item); // Add element to the result
            }
        }
    };

    helper(arr, 0); // Start flattening with depth 0
    return result;
};

// Example usage:
const arr = [1, [2, [3, [4, [5]]]], 6];
const depth = 2;

console.log(flat(arr, depth)); 
// Output: [1, 2, 3, [4, [5]], 6]

```
['JavaScript']