Chunk Array

EasyData EngineeringArray

Description

Given an array and a positive batch size k, split the array into consecutive chunks of size k. The final chunk holds the remaining elements when the length is not divisible by k.

Examples

Input:[1,2,3,4,5], 2
Output:[[1,2],[3,4],[5]]
Explanation:

The array is sliced into consecutive blocks of the given size, with the final block holding whatever remains.

Input:[1,2,3,4], 2
Output:[[1,2],[3,4]]
Explanation:

The array is sliced into consecutive blocks of the given size, with the final block holding whatever remains.

Input:[1,2,3], 1
Output:[[1],[2],[3]]
Explanation:

The array is sliced into consecutive blocks of the given size, with the final block holding whatever remains.

Constraints

  • 1 ≤ array length ≤ 10⁴
  • 1 ≤ k ≤ array length

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.