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

<p>Given an integer array <code>hand</code> where <code>hand[i]</code> is the value written on the <code>i<sup>th</sup></code> card and an integer <code>groupSize</code>, return <code>true</code> if she can rearrange the cards, or <code>false</code> otherwise.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> hand = [1,2,3,6,2,3,4,7,8], groupSize = 3
<strong>Output:</strong> true
<strong>Explanation:</strong> Alice&#39;s hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> hand = [1,2,3,4,5], groupSize = 4
<strong>Output:</strong> false
<strong>Explanation:</strong> Alice&#39;s hand can not be rearranged into groups of 4.

</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= hand.length &lt;= 10<sup>4</sup></code></li>
	<li><code>0 &lt;= hand[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>1 &lt;= groupSize &lt;= hand.length</code></li>
</ul>

<p>&nbsp;</p>
<p><strong>Note:</strong> This question is the same as 1296: <a href="https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/" target="_blank">https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/</a></p>

#include <vector>
#include <queue>
#include <unordered_map>

class Solution {
    struct myCmp {
        bool operator()(int const& lhs, int const& rhs) const {
            return lhs > rhs;  // Min-heap (smallest element at the top)
        }
    };

public:
    bool isNStraightHand(std::vector<int>& hand, int groupSize) {
        int handSize = hand.size();
        if (handSize % groupSize != 0) {
            return false;  // If the total number of cards isn't divisible by groupSize, return false
        }

        // Create a priority queue (min-heap) using custom comparator
        std::priority_queue<int, std::vector<int>, myCmp> pq(hand.begin(), hand.end());

        // Frequency map to count occurrences of each card
        std::unordered_map<int, int> freq;
        for (auto x : hand) {
            freq[x]++;
        }

        // Process each card in the priority queue
        while (!pq.empty()) {
            int startCard = pq.top();  // Get the smallest available card
            pq.pop();

            if (freq[startCard] == 0) {
                continue;  // Skip cards that have already been used
            }

            // Try to form a group starting from startCard
            for (int i = 0; i < groupSize; ++i) {
                int currentCard = startCard + i;
                if (freq[currentCard] == 0) {
                    return false;  // Cannot form a valid group
                }
                freq[currentCard]--;  // Use one occurrence of currentCard
            }
        }

        return true;  // All groups are successfully formed
    }
};

31395