Groups of K
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
0 <= len(nums) <= 100-1000 <= nums[i] <= 10001 <= k <= 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Arrays problems
- Items Within BudgetBeginner
- Kids With the Most CandiesBeginner
- Largest At Least Twice OthersBeginner
- Longest Increasing RunBeginner
- Max Consecutive OnesBeginner
- Peak So FarBeginner
- Running Sum of an ArrayBeginner
- Second AppearanceBeginner