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.

&nbsp;
Example 1:

Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].


Example 2:

Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].


Example 3:

Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.


&nbsp;
Constraints:


	1 &lt;= k &lt;= nums.length &lt;= 105
	1 &lt;= nums[i] &lt;= 109


&nbsp;
Note: This question is the same as&nbsp;846:&nbsp;https://leetcode.com/problems/hand-of-straights/
class Solution(object):
    def isPossibleDivide(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: bool
        """
        if len(nums) % k != 0:
            return False
        
        freq = collections.defaultdict(int)
        for v in nums:
            freq[v] += 1
            
        nums.sort()
        while sum(freq.values()) > 0:
			# smallest number in sorted nums with freq > 0 to start a sequence [v, v+1, v+2 ... , v+k-1]
            for v in nums: 
                if freq[v] > 0: 
                    for i in range(k):
                        if freq[v+i] <= 0:
                            return False
                        else:
                            freq[v+i] -= 1
        return True
class Solution {
    public boolean isPossibleDivide(int[] nums, int k) {
        if (nums.length % k != 0) return false;
        Map<Integer, Integer> countMap = new HashMap<>();
        for (int num : nums) {
            int count = countMap.getOrDefault(num, 0);
            countMap.put(num , count + 1);
        }
        Arrays.sort(nums);
        for (int num : nums) {
            if (!countMap.containsKey(num)) continue;
            int count = countMap.get(num);
            if (count == 1) countMap.remove(num);
            else countMap.put(num, count - 1);
            for (int i = 1; i < k; i++) {
                int next = num + i;
                if (!countMap.containsKey(next)) return false;
                count = countMap.get(next);
                if (count == 1) countMap.remove(next);
                else countMap.put(next, count - 1);
            }
        }
        return true;
    }
}
class Solution {
public:
    bool isPossibleDivide(vector<int>& nums, int k) {
        if(nums.size() % k) return false;
        
        map<int, int> m;
        for(int i : nums) m[i]++;
        
        int n = m.size();
        while(n) {
            int a = m.begin() -> first;
            m[a]--;
            if(!m[a]) m.erase(a), n--;
            for(int i=1; i<k; i++) {
                if(m.find(a + i) == m.end()) return false;
                m[a + i]--;
                if(!m[a + i]) m.erase(a + i), n--;
            }
        }
        
        return true;
    }
};
var isPossibleDivide = function(nums, k) {
    if(nums.length % k) {
        return false;
    }

    nums.sort((a, b) => a - b);

    let numberOfArrays = nums.length / k, index = 0, dp = Array(numberOfArrays).fill(null).map(() => []);

    dp[0].push(nums[0]);

    for(let i = 1; i < nums.length; i++) {
        if(nums[i] === nums[i - 1]) {
            if(index === numberOfArrays - 1) {
                return false;
            }
            index++;
        }
        else {
            index = 0;
            while(dp[index].length === k) {
                index++;
            }
        }
        if(dp[index].length && dp[index].at(-1) + 1 != nums[i]) {
            return false;
        }
        dp[index].push(nums[i])
    }

    return true;
};
Divide Array in Sets of K Consecutive Numbers