<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the number of <strong>good subarrays</strong> of </em><code>nums</code>.</p>

<p>A <strong>good array</strong> is an array where the number of different integers in that array is exactly <code>k</code>.</p>

<ul>
	<li>For example, <code>[1,2,3,1,2]</code> has <code>3</code> different integers: <code>1</code>, <code>2</code>, and <code>3</code>.</li>
</ul>

<p>A <strong>subarray</strong> is a <strong>contiguous</strong> part of an array.</p>

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

<pre>
<strong>Input:</strong> nums = [1,2,1,2,3], k = 2
<strong>Output:</strong> 7
<strong>Explanation:</strong> Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2]
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,2,1,3,4], k = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= nums[i], k &lt;= nums.length</code></li>
</ul>

class Solution {
public:
    int subarraysWithKDistinct(vector<int>& nums, int k) {
        vector<int>longest_sub(nums.size(),0); //longest subarray starting at i with k distinct integers
        vector<int>shortest_sub(nums.size(),0);
        unordered_map<int,int>mp;
        int unique=0;
        int st=0;
        // int end=0;
        for(int end =0;end< nums.size();end++)
        {
            if(mp[nums[end]]==0)
            {
                //new number
                unique++;
                mp[nums[end]]++;
                if(unique==k)
                {
                    //shortest array for this start point
                    shortest_sub[st] = end-st+1;
                    //we found our solution, now move st ahead
                    while(st <= end)
                    {
                        mp[nums[st]]--;
                        
                        if(mp[nums[st]]==0)
                        {
                            unique--;
                            st++;
                            break;
                        }
                        else
                        {
                            //unique stay same
                            st++;
                            shortest_sub[st] = end-st+1;

                        }
                    }
                }
            }
            else
            {
                //number repeating
                mp[nums[end]]++;

            }
        }

        //now for longest_sub
        mp.clear();
        unique=0;
        st=0;
        int end=0;
        for(;end< nums.size();end++)
        {
            if(mp[nums[end]]==0)
            {
                //new number
                unique++;
                mp[nums[end]]++;
                if(unique==k+1)
                {
                    //current point gave us one extra unique so we have a solution from st to end-1
                    longest_sub[st] = end-st;
                    //we found  our solution, now move st ahead 
                    while(st < end)
                    {
                        mp[nums[st]]--;
                        
                        if(mp[nums[st]]==0)
                        {
                            unique--;
                            st++;
                            break;
                        }
                        else
                        {
                            //unique stay same
                            st++;
                            longest_sub[st] = end-st;

                        }
                    }
                }
            }
            else
            {
                //number repeating
                mp[nums[end]]++;

            }
        }
        while(unique==k && st < end)
        {
            //we never got to k+1  because array finished
            longest_sub[st] = end-st;
            mp[nums[st]]--;
            if(mp[nums[st]]==0)
            unique--;
            st++;

        }
        
        for(int i=0;i<nums.size();i++)
        cout<<shortest_sub[i]<<" ";
        cout<<"fgfg ";
        for(int i=0;i<nums.size();i++)
        cout<<longest_sub[i]<<" ";
        int ans=0;
        for(int i=0;i<nums.size();i++)
        {
            if(longest_sub[i]!=0)
            ans = ans + (longest_sub[i] - shortest_sub[i] + 1);
        }

        return ans;
    }
};
53623