Back to blog
Sep 10, 2024
5 min read

Chunk Array

Split an array into chunks of a given size, with the last chunk potentially containing fewer elements.

Difficulty: Easy | Acceptance: 84.60% | Paid: No Topics: N/A

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 cannot be divided evenly.

Examples

Example 1

Input:

arr = [1,2,3,4,5], size = 1

Output:

[[1],[2],[3],[4],[5]]

Explanation: The arr has been split into subarrays each with 1 element.

Example 2

Input:

arr = [1,9,6,3,2], size = 3

Output:

[[1,9,6],[3,2]]

Explanation: The arr has been split into subarrays with 3 elements. However, only two elements are left for the 2nd subarray.

Example 3

Input:

arr = [8,5,3,2,6], size = 6

Output:

[[8,5,3,2,6]]

Explanation: Size is greater than arr.length thus all elements are in the first subarray.

Example 4

Input:

arr = [], size = 1

Output:

[]

Explanation: There are no elements to be chunked so an empty array is returned.

Constraints

- arr is a string representing the array.
- 2 <= arr.length <= 10^5
- 1 <= size <= arr.length + 1

Iterative Slicing

Intuition Traverse the array with a step equal to the chunk size, extracting slices from the current index to the current index plus the chunk size.

Steps

  • Initialize an empty result array
  • Loop through the array with step size
  • Slice the array from current index to current index + size
  • Append each slice to the result array
python
class Solution:\n    def chunk(self, arr: List[int], size: int) -> List[List[int]]:\n        result = []\n        for i in range(0, len(arr), size):\n            result.append(arr[i:i+size])\n        return result

Complexity

  • Time: O(n) where n is the length of the array
  • Space: O(n) for storing the result
  • Notes: Simple and efficient approach with minimal overhead

Recursive Approach

Intuition Recursively process the array by taking the first size elements as a chunk and then recursively chunking the remaining elements.

Steps

  • Base case: if array is empty, return empty array
  • Take first size elements as a chunk
  • Recursively chunk the remaining elements
  • Combine current chunk with recursive result
python
class Solution:\n    def chunk(self, arr: List[int], size: int) -> List[List[int]]:\n        if not arr:\n            return []\n        return [arr[:size]] + self.chunk(arr[size:], size)

Complexity

  • Time: O(n) where n is the length of the array
  • Space: O(n) for result plus O(n/size) for recursion stack
  • Notes: Elegant but may cause stack overflow for very large arrays

Built-in Methods

Intuition Leverage language-specific built-in methods or functional programming constructs to create chunks in a more declarative style.

Steps

  • Calculate the number of chunks needed
  • Use built-in methods to create each chunk
  • Return the resulting array of chunks
python
class Solution:\n    def chunk(self, arr: List[int], size: int) -> List[List[int]]:\n        return [arr[i:i+size] for i in range(0, len(arr), size)]

Complexity

  • Time: O(n) where n is the length of the array
  • Space: O(n) for storing the result
  • Notes: Most concise and idiomatic approach for each language