All problems
0877MediumArrayHash TableGreedySorting

Splitting the Reels Into Runs

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1296Divide Array in Sets of K Consecutive Numbers

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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