# 846. Hand of Straights

## Problem Statement

\
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.

&#x20;

**Example 1:**

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

**Example 2:**

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

&#x20;

**Constraints:**

* `1 <= hand.length <= 104`
* `0 <= hand[i] <= 109`
* `1 <= groupSize <= hand.length`

&#x20;

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

## Intuition

```
Apprach:
We keep track of minimum element using minheap
And then from that minimum element try to find
Using loop that if val+k elements exist

Now, if freq of element goes to zero, we pop in heap


But if 
1 -> 1
2-> 0 

Now, 1 has freq, but we will need 2, so we return false
```

### Links

<https://leetcode.com/problems/hand-of-straights/description/>

### Video Links

<https://www.youtube.com/watch?v=amnrMCVd2YI&t=8s&ab_channel=NeetCode>

### Approach 1:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    bool isNStraightHand(vector<int>& hand, int size) {
        if(hand.size() % size != 0)
            return false;
        
        unordered_map<int,int> mp;
        priority_queue<int, vector<int>, greater<int>> pq;
        for(auto &it: hand)
            mp[it]++;

        for(auto &it: mp)
            pq.push(it.first);

        while(!pq.empty()){
            int val = pq.top();
            for(int i=0; i<size; i++){
                if(mp.find(val+i) == mp.end())
                    return false;

                mp[val+i]--;
                if(mp[val+i] == 0){
                    if(pq.top() != i+val)
                        return false;

                    pq.pop();
                }
            }
        }

        return true;
    }
};
```

{% endcode %}

### Approach 2:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://coding-9.gitbook.io/untitled/heaps-and-priority-queue/846.-hand-of-straights.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
