FunctionRacerProblemsRace

Groups of K

BeginnerArraysPython, JavaScript, TypeScript, Java, C++

Split nums into consecutive groups of k items each, keeping the original order: the first k items form the first group, the next k items the second group, and so on. The last group may hold fewer than k items when the list does not divide evenly.

Return the list of groups. An empty nums gives an empty list.

Example 1

nums [1,2,3,4,5,6,7]
k 3
returns [[1,2,3],[4,5,6],[7]]

Seven items in groups of three: two full groups and a last group with the single leftover 7.

Example 2

nums [1,2,3,4]
k 2
returns [[1,2],[3,4]]

Four items split evenly into two pairs.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Arrays problems