Given an array of integers nums and a positive integer k, check whether it is possible to divide this array into sets of k consecutive numbers.
Return True if it is possible. Otherwise, return False.
Function Signature
def is_possible_divide(nums: list[int], k: int) -> bool:
Rules
-
A set of
k
consecutive numbers has the form
x, x + 1, ..., x + k - 1
for some integer
x
. The order of elements in
nums
does not matter.
-
Every array position belongs to exactly one set, so a value that appears several times can go into several different sets.
Constraints
-
1 <= k <= len(nums) <= 10^5
-
1 <= nums[i] <= 10^9
Examples
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: True
The array can be divided into [1,2,3,4] and [3,4,5,6], which uses all eight elements once.
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: True
The array can be divided into [1,2,3], [2,3,4], [3,4,5] and [9,10,11].
Input: nums = [1,2,3,5,6,7,8,9], k = 4
Output: False
The length 8 is a multiple of 4, but 1 is the smallest value, so its set would have to be [1,2,3,4], and the array has no 4.