Problem · Array
Partition Into K-Size Consecutive Groups
Learn this problemProblem statement
Given an integer array nums and a positive integer k, determine whether every array element can be used exactly once to form groups of exactly k values.
Within each group, the values can be reordered as v, v + 1, ..., v + k - 1. Duplicate values are separate multiset elements. Return true if such a partition exists; otherwise return false.
Function
canPartitionConsecutive(nums: int[], k: int) → booleanExamples
Example 1
nums = [1,2,3,6,2,3,4,7,8]k = 3return = trueThe groups can be [1,2,3], [2,3,4], and [6,7,8].
Example 2
nums = [1,2,3,4,5]k = 4return = falseFive elements cannot be divided into groups of exactly four.
Constraints
1 <= nums.length <= 10^5.1 <= k <= nums.length.-10^9 <= nums[i] <= 10^9.