Alice has some number of cards and she wants to rearrange the cards into groups so that each group is of size groupSize, and consists of groupSize consecutive cards.

Given an integer array hand where hand[i] is the value written on the ith card and an integer groupSize, return true if she can rearrange the cards, or false otherwise.

&nbsp;
Example 1:

Input: hand = [1,2,3,6,2,3,4,7,8], groupSize = 3
Output: true
Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]


Example 2:

Input: hand = [1,2,3,4,5], groupSize = 4
Output: false
Explanation: Alice's hand can not be rearranged into groups of 4.



&nbsp;
Constraints:


	1 &lt;= hand.length &lt;= 104
	0 &lt;= hand[i] &lt;= 109
	1 &lt;= groupSize &lt;= hand.length


&nbsp;
Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/

#####################################################################################################################
# Problem: Hand of Straights
# Solution : Hash Table, Min Heap
# Time Complexity : O(n logn)
# Space Complexity : O(n)
#####################################################################################################################

class Solution:
    def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:
        if len(hand) % groupSize:
            return False

        freq = collections.defaultdict(int)

        for num in hand:
            freq[num] += 1

        min_heap = list(freq.keys())
        heapq.heapify(min_heap)

        while min_heap:
            smallest = min_heap[0]
            for num in range(smallest, smallest + groupSize):
                if num not in freq:
                    return False
                freq[num] -= 1

                if freq[num] == 0:
                    if num != min_heap[0]:
                        return False
                    heapq.heappop(min_heap)
        return True
class Solution {
    public boolean isNStraightHand(int[] hand, int groupSize) {
        if(hand.length % groupSize != 0)
            return false;

        Map<Integer, Integer> map = new HashMap<>();
        PriorityQueue<Integer> minHeap = new PriorityQueue<>();

        for(int card : hand){
            if(map.containsKey(card))
                map.put(card, map.get(card) + 1);
            else {
                map.put(card, 1);
                minHeap.add(card);
            }
        }

        while(!minHeap.isEmpty()){
            int min = minHeap.peek();
            for(int i=min; i < min + groupSize; i++){
                if(!map.containsKey(i) || map.get(i) == 0)
                    return false;
                map.put(i, map.get(i) - 1);
                if(map.get(i) == 0){
                    if(minHeap.peek() != i)
                        return false;
                    minHeap.poll();
                }
            }
        }
        return true;
    }
}
class Solution {
public:
    bool isNStraightHand(vector<int>& hand, int groupSize) {
        int n = hand.size();
        if(n%groupSize != 0)   // We can't rearrange elements of size groupsize so return false
            return false;
        map<int,int>mp;
        for(auto it : hand)
            mp[it]++;
        while(mp.size() > 0){
            int start = mp.begin()->first;   // Taking the topmost element of map

            for(int i=0; i<groupSize; i++){
                if(mp.find(start) != mp.end()){
                    mp[start]--;
                    if(mp[start] == 0)
                        mp.erase(start);
                    start++;                // Increasing for finding next consecutive element of start. 
                }
                else
                    return false;
            }
        }
        return true;
    }
};
var isNStraightHand = function(hand, groupSize) {
    if(hand.length%groupSize!==0) return false;
	
    const map = new Map();
    hand.forEach(h=>{
        map.set(h,map.get(h)+1||1);
    });
    
	// sort based on the key in asc order
    const sortedMap = new Map([...map.entries()].sort((a,b)=>a[0]-b[0]));
    
    while(sortedMap.size){
	// getting the first key
        const firstKey = sortedMap.keys().next().value; 
        for(let i=firstKey;i<firstKey+groupSize;++i){
            if(!sortedMap.has(i)){
                return false
            }
            const freq = sortedMap.get(i);
            if(freq===1){
                sortedMap.delete(i)
            }else{
                sortedMap.set(i,sortedMap.get(i)-1);
            }
        }
    }
    return true;
};
Hand of Straights