Divide Array in Sets of K Consecutive Numbers

Given an array and an integer k, determine whether the array can be divided into sets of k consecutive numbers. A frequency-aware counting strategy over the sorted values is the standard solution.

Input Format

nums = array of integers, k = size of each consecutive group

Output Format

true if the array can be divided into consecutive sets of size k

Constraints

  • 1 <= nums.length <= 10^5; 1 <= k <= nums.length; nums.length is divisible by k; 0 <= nums[i] <= 10^5

Examples

Example 1:

Input:

nums = [1,2,3,3,4,4,5,6]
k = 4

Output:

true

Explanation:

The array can be split into [1,2,3,4] and [3,4,5,6].

Example 2:

Input:

nums = [3,3,2,2,1,1]
k = 3

Output:

true

Explanation:

Two groups [1,2,3] can be formed.

Loading...
Divide Array in Sets of K Consecutive Numbers