Divide Array in Sets of K Consecutive Numbers
Real-world version: Splitting the Reels Into RunsThe 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.
def split_into_runs(nums: list[int], k: int) -> bool:public boolean splitIntoRuns(int[] nums, int k)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 12 test cases: 12 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Make Array Zero by Subtracting Equal AmountsReal-world version: Rounds Needed to Clear Every ReadingEasy
- Task SchedulerReal-world version: Press Die CooldownMedium
- Maximum Palindromes After OperationsReal-world version: Noticeboard Word StripsMedium
- Hand of StraightsReal-world version: Filing Journal Issues Into Display RunsMedium