{ "data": { "question": { "questionId": "2759", "questionFrontendId": "2625", "boundTopicId": null, "title": "Flatten Deeply Nested Array", "titleSlug": "flatten-deeply-nested-array", "content": "

Given a multi-dimensional array arr and a depth n, return a flattened version of that array.

\n\n

A multi-dimensional array is a recursive data structure that contains integers or other multi-dimensional arrays.

\n\n

flattened array is a version of that array with some or all of the sub-arrays removed and replaced with the actual elements in that sub-array. This flattening operation should only be done if the current depth of nesting is greater than n. The depth of the elements in the first array are considered to be 0.

\n\n

Please solve it without the built-in Array.flat method.

\n\n

 

\n

Example 1:

\n\n
\nInput\narr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]\nn = 0\nOutput\n[1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]\n\nExplanation\nPassing a depth of n=0 will always result in the original array. This is because the smallest possible depth of a subarray (0) is not less than n=0. Thus, no subarray should be flattened. 
\n\n

Example 2:

\n\n
\nInput\narr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]\nn = 1\nOutput\n[1, 2, 3, 4, 5, 6, 7, 8, [9, 10, 11], 12, 13, 14, 15]\n\nExplanation\nThe subarrays starting with 4, 7, and 13 are all flattened. This is because their depth of 0 is less than 1. However [9, 10, 11] remains unflattened because its depth is 1.
\n\n

Example 3:

\n\n
\nInput\narr = [[1, 2, 3], [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]\nn = 2\nOutput\n[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]\n\nExplanation\nThe maximum depth of any subarray is 1. Thus, all of them are flattened.
\n\n

 

\n

Constraints:

\n\n\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Medium", "likes": 8, "dislikes": 2, "isLiked": null, "similarQuestions": "[{\"title\": \"JSON Deep Equal\", \"titleSlug\": \"json-deep-equal\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Convert Object to JSON String\", \"titleSlug\": \"convert-object-to-json-string\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "exampleTestcases": "[1,2,3,[4,5,6],[7,8,[9,10,11],12],[13,14,15]]\n0\n[1,2,3,[4,5,6],[7,8,[9,10,11],12],[13,14,15]]\n1\n[1,2,3,[4,5,6],[7,8,[9,10,11],12],[13,14,15]]\n2", "categoryTitle": "JavaScript", "contributors": [], "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {any[]} arr\n * @param {number} depth\n * @return {any[]}\n */\nvar flat = function (arr, n) {\n \n};", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "type MultiDimensionalArray = (number | MultiDimensionalArray)[];\n\nvar flat = function (arr: MultiDimensionalArray, n: number): MultiDimensionalArray {\n \n};", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"383\", \"totalSubmission\": \"573\", \"totalAcceptedRaw\": 383, \"totalSubmissionRaw\": 573, \"acRate\": \"66.8%\"}", "hints": [ "Write a recursive function that keeps track of the current depth.", "if the current depth >= the maximum depth, always just push the value to the returned array. Otherwise recursively call flat on the array." ], "solution": null, "status": null, "sampleTestCase": "[1,2,3,[4,5,6],[7,8,[9,10,11],12],[13,14,15]]\n0", "metaData": "{\n \"classname\": \"Array\",\n \"constructor\": {\n \"params\": []\n },\n \"methods\": [],\n \"return\": {\n \"type\": \"boolean\"\n },\n \"systemdesign\": true,\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "enableTestMode": false, "enableDebugger": false, "envInfo": "{\"javascript\": [\"JavaScript\", \"

Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES6 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\\r\\n\\r\\n

For Priority Queue / Queue data structures, you may use 5.3.0 version of datastructures-js/priority-queue and 4.2.1 version of datastructures-js/queue.

\"], \"typescript\": [\"Typescript\", \"

TypeScript 4.5.4, Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES2020 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }