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.
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 valid JSON array2 <= JSON.stringify(arr).length <= 1051 <= size <= arr.length + 1The goal of #2677 Chunk Array is to divide a given array into smaller groups (chunks) of a specified size. Each chunk should contain at most size elements, and the last chunk may contain fewer elements if the array length is not perfectly divisible.
A common approach is to iterate through the array while grouping elements into subarrays. You can move through the array in steps of size and collect a slice of elements for each step. Each slice becomes a new chunk that is added to the result list. This method ensures that elements are processed sequentially and placed into the correct group.
Another variation is to build chunks dynamically by adding elements until the current chunk reaches the required size, then pushing it into the result. Both strategies rely on simple array traversal.
Since each element is visited only once, the algorithm is efficient with linear time complexity. The extra space used corresponds to the output chunks created during processing.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Iterative chunking using slicing or step traversal | O(n) | O(n) |
Sahil & Sarra
This approach involves iterating over the array using a loop and extracting subarrays using slicing. The loop increments by the chunk size in each iteration, thus effectively slicing the array into chunks of the desired size.
Time Complexity: O(n), where n is the number of elements in the array.
Space Complexity: O(n), storing the entire chunked array requires space proportional to the input size.
1using System;
2using System.Collections.Generic;
3
4public class ChunkArray {
5 public static List<List<int>> Chunk(int[] arr, int size) {
6 List<List<int>> chunked = new List<List<int>>();
7 for (int i = 0; i < arr.Length; i += size) {
8 List<int> chunk = new List<int>();
9 for (int j = i; j < i + size && j < arr.Length; j++) {
10 chunk.Add(arr[j]);
11 }
12 chunked.Add(chunk);
13 }
14 return chunked;
15 }
16}The Chunk method iterates through the input array with a step of size. It collects elements into a list chunk until reaching the specified size or the end of the array, after which the chunk is added to the result list.
This approach uses simple arithmetic operations to determine when to create a new subarray. Using the modulus operator allows checking if the number of currently collected elements is equal to the chunk size, upon which a new subarray is started.
Time Complexity: O(n), where n is the number of elements in the array.
Space Complexity: O(n), because the additional space needed is proportional to the input size.
1Watch expert explanations and walkthroughs
Jot down your thoughts, approach, and key learnings
Yes, variations of array chunking problems appear in coding interviews, especially for testing basic array manipulation and iteration skills. They are common in entry-level or easy-level interview questions.
The optimal approach is to iterate through the array and group elements into subarrays of the given size. This can be done using index stepping or array slicing. Each element is processed once, making the time complexity linear.
A dynamic array or list is ideal for storing the resulting chunks. Each chunk is itself a subarray that holds up to the specified number of elements. This structure makes it easy to append and manage grouped elements.
The time complexity is O(n) because the algorithm processes each element in the input array exactly once. Additional operations like slicing or pushing chunks are proportional to the number of elements.
The method keeps appending elements to the current chunk list until its length reaches size, at which point the chunk is added to the chunked result list. If there is a leftover partial chunk at the end, it is also added to the result.