Show problems as
Classic problem #1296MediumArrayHash TableGreedySorting

Divide Array in Sets of K Consecutive Numbers

Real-world version: Splitting the Reels Into Runs
Tracked in this browser only
Read Divide Array in Sets of K Consecutive Numbers on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A batch of film reels is logged as nums, where nums[i] is the length of reel i in metres. Every reel must go into a canister, and each canister must hold exactly k reels whose lengths are k consecutive whole numbers, in some order.

Return true when the batch can be split up that way, and false otherwise.

Examples

Example 1

Input
nums = [4, 5, 6, 7, 8, 9], k = 3
Output
true

One canister takes the reels of 4, 5 and 6 metres and the other takes 7, 8 and 9, and each holds three consecutive lengths.

Example 2

Input
nums = [7, 8, 9, 7, 8, 10], k = 3
Output
false

One canister must take the reels of 7, 8 and 9 metres, which leaves 7, 8 and 10, and those three are not consecutive.

Example 3

Input
nums = [2, 2, 3, 3, 4, 4], k = 3
Output
true

Two canisters each take reels of 2, 3 and 4 metres.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= k <= 100000
  • 1 <= nums[i] <= 10^9
  • k is at most nums.length

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def split_into_runs(nums: list[int], k: int) -> bool:
Java
public boolean splitIntoRuns(int[] nums, int k)