Write a function which Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers Return True if its possible otherwise return False


Topic: Write a function which Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers Return True if its possible otherwise return False

Solution

import collections
def isPossibleDivide(nums, k):
 
    d = collections.Counter(nums)
    for num in sorted(d.keys()):
        if num in d:
            for i in range(k - 1, -1, -1):
                d[num + i] -= d[num] 
                if d[num + i] == 0:
                    del d[num + i]
                if d[num + i] < 0:
                    return False
    return (True if not d else False)
	
	



List all Python Programs