{ "data": { "question": { "questionId": "2798", "questionFrontendId": "2677", "boundTopicId": null, "title": "Chunk Array", "titleSlug": "chunk-array", "content": "
Given an array arr
and a chunk size size
, return a chunked array. A chunked array contains the original elements in arr
, but consists of subarrays each of length size
. The length of the last subarray may be less than size
if arr.length
is not evenly divisible by size
.
You may assume the array is the output of JSON.parse
. In other words, it is valid JSON.
Please solve it without using lodash's _.chunk
function.
\n
Example 1:
\n\n\nInput: arr = [1,2,3,4,5], size = 1\nOutput: [[1],[2],[3],[4],[5]]\nExplanation: The arr has been split into subarrays each with 1 element.\n\n\n
Example 2:
\n\n\nInput: arr = [1,9,6,3,2], size = 3\nOutput: [[1,9,6],[3,2]]\nExplanation: The arr has been split into subarrays with 3 elements. However, only two elements are left for the 2nd subarray.\n\n\n
Example 3:
\n\n\nInput: arr = [8,5,3,2,6], size = 6\nOutput: [[8,5,3,2,6]]\nExplanation: Size is greater than arr.length thus all elements are in the first subarray.\n\n\n
Example 4:
\n\n\nInput: arr = [], size = 1\nOutput: []\nExplanation: There are no elements to be chunked so an empty array is returned.\n\n
\n
Constraints:
\n\narr
is a valid JSON array2 <= JSON.stringify(arr).length <= 105
1 <= size <= arr.length + 1
Node.js 16.13.2
.
Your code is run with --harmony
flag, enabling new ES6 features.
lodash.js library is included by default.
\\r\\n\\r\\nFor 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 5.1.6, Node.js 16.13.2
.
Your code is run with --harmony
flag, enabling new ES2022 features.
lodash.js library is included by default.
\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }