Flatten array
IntuitionFirstly, we need to understand, what way we will use - recursive or loop, and start implement what we want.Approach
As we can see, if n===0, we can ret
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Firstly, we need to understand, what way we will use - recursive or loop, and start implement what we want.

# Approach
<!-- Describe your approach to solving the problem. -->
1. As we can see, if `n===0`, we can return given array as is, so we can make a check for it.
2. Create a result array, whitch will be returned.
3. We will need a helper function, which will be called recursively
It will accept an array, and a current depth, to check, if `maxDepth`
exeeded.
4. Now we need to check - if `item` is a number, or if `maxDepth` is exeeded - in both ways we need to push an element to the result array.
5. If item is an array, and `maxDepth` is not exeeded - we need to go deeper, to call our helper one more time.
6. Enjoy recision magic)))) 



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


# Code
```typescript []
type MultiDimensionalArray = (number | MultiDimensionalArray)[];

const flat =  (arr: MultiDimensionalArray, maxDepth: number): MultiDimensionalArray => {
<!-- CHECK IF WE DONT NEED ANY ADDITIONAL ACTIONS -->
    if(!n) return arr
    
<!-- CREATE A RESULT ARRAY -->
    let result = []

<!-- CREATE HELPER FUNCTION -->
    const flatten = (item, depth) => {

<!-- CHECH MAXIMUM DEPTH AND ARRAY -->
        if (Array.isArray(item) && depth <= maxDepth  ) {
            item.forEach(el => {
<!-- CALL RECURSIVELY -->
                flatten(el, depth + 1)
            })
        } else {
<!-- PUSH TO RESULT IF NUMBER OR DEPTH IS EXEEDED -->
            result.push(item)
        }
    }
<!-- CALL HELPER FIRST TIME -->
    flatten(arr,0)

    return result
};


```
['TypeScript']