Simple Solution with explanation of the program and approach to solve the problem step by step
IntuitionUnderstand the given condition and think the logic to the given problem and the return value.ApproachWe have to flatten the array until the depth n wit
- - -
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Understand the given condition and think the logic to the given problem and the return value.
- - -
# Approach
<!-- Describe your approach to solving the problem. -->
We have to flatten the array until the depth `n` without using the built in function flat().But starting depth must be 0.
- - -
# Problem Breakdown
We just have to flatten the array without using the flat() until depth is `n`. If `n` is 1 we have to flatter array only for depth 1 we are not exced the depth more then `n`.Then Return that array.
- - -
# Program Breakdown
**THIS IS THE BREAKDOWN OF BELOW PROGRAM**
- We need a array to store the result so we use `res` array.
- I need a function that can help in Recursion which take 2 argument `item` which is element of the array,`depth` is a depth we reached in flattering the array,that is `function flatter(item,depth)`.
- Now let's skip the function and move to `for (let i = 0; i < arr.length; i++){flatter(arr[i], 0)}` for better understand how the program run
- The above for loop is the loop present in the end of the function `flat`.
- Which visit all elemnts of array and pass it to `flatter` function for the next process and makesure that no element is skip. which pass array element `arr[i]` and `0` as depth.
- Now back to the function `flatter`, `item` is array element and `depth` is how depth we want to flatter the arary.
- `if (Array.isArray(item) && depth < n)` check that `item` is a sub array or just a element by `Array.isArray(item)` and `depth < n` make sure that depth not exceed the `n`, if 1 of the conditon fail that means `depth` has reach to `n` or `item` is not a sub array,If array is not sub array we move to `else` block and push it to result array by `res.push(item)`.
- If `item` is sub array and `depth` npt reach `n` we have do the same process for all the elements of `item` until 1 of the 2 condition fails.
- If any 1 of the 2 condition fails in `if (Array.isArray(item) && depth < n)` Out mission is over.
- oh no we have to return the result array `res` by `return res` now its over.
- - -
# Complexity
- Time complexity: $$O(m)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
The time complexity of the `flat` function can be analyzed based on the number of elements in the input array and the depth of nesting. Let `m` be the total number of elements in the input array (including all nested arrays)
- Space complexity: $$O(m + n)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
The space complexity is determined by the space required to store the result array `res` and the call stack used for recursion.The result array will store all elements up to the specified depth, which can also be O(m) in the worst case. The maximum depth of the recursion stack can go up to n, leading to a space complexity of O(n) for the call stack. Therefore, the overall space complexity is O(m + n).
- - -
# Code
```javascript []

var flat = function (arr, n) {

    let res = []

    function flatter(item, depth) {

        if (Array.isArray(item) && depth < n) {
            for (let i = 0; i < item.length; i++) {
                flatter(item[i], depth + 1)
            }
        }
        else {
            res.push(item)
        }
    }

    for (let i = 0; i < arr.length; i++) {
        flatter(arr[i], 0)
    }

    return res
};


```
- --
# Have a great time in coding see you soon.....!
- - -
['JavaScript']