<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 subarrayswithatmostk(vector<int>& nums, int k){
        if(k==0)return 0;
        int ans=0;
        int n=nums.size();
        int freq[n+1];memset(freq,0,sizeof(freq));
        int distinct=0,st=0;
        for(int i=0;i<nums.size();i++){
            if(freq[nums[i]]==0){
                distinct++;
                if(distinct==k+1){
                    while(1){
                        cout<<st<<endl;
                        freq[nums[st]]--;
                        if(freq[nums[st]]==0){st++;distinct--;break;}
                        st++;
                    }
                }
            }
            ans=ans+i-st+1;
            freq[nums[i]]++;
        }
        return ans;
    }
    int subarraysWithKDistinct(vector<int>& nums, int k) {
        return subarrayswithatmostk(nums,k)-subarrayswithatmostk(nums,k-1);
    }
};

/*

class Solution {
public:
    int subarrk(vector<int>&v,int k)
    {
        int n=v.size(),ans=0;
        unordered_map<int,int>mp;
        int l=0,r=0,d=0;
        //mp[v[0]]++;
        for(int i=0;i<n;i++)
        {
            if(mp[v[i]]==0)
            {
                d++;mp[v[i]]++;
                if(d>k)
                {
                    while(l<n && d>k)
                    {
                        if(mp[v[l]]==1)
                        {
                            mp[v[l]]--;d--;l++;
                        }
                        else 
                        {
                            mp[v[l]]--;l++;
                        }
                    }
                }
                ans=ans+i-l+1;
            }
            else
            {
                mp[v[i]]++;
                ans=ans+i-l+1;
            }
        }
        return ans;
    }
    int subarraysWithKDistinct(vector<int>& s, int k) {
        	int n=s.size();
            int a1=subarrk(s,k);
            int a2=subarrk(s,k-1);
            return a1-a2;


    	// //if(k==1){return (n*(n+1))/2;}
    	// //int freq[26]={0};
		// 	map<int,int>freq;
    	// int l=0,r=0,d=0;
    	// int long long ans=0;
    	// //freq[s[0]-'a']++;
    	// for(int i=0;i<n;i++)
    	// {
    	//     if(freq[s[i]]==0)
    	//     {
    	//         d++;freq[s[i]]++;
    	//         if(d==k)
    	//         {
    	//             int a=0,b=0,c=i;i++;
    	//             while(i<n && freq[s[i]]!=0){b++;i++;}//freq[s[i]-'a']++;
    	//             while(l<n && freq[s[l]]!=1){freq[s[l]]--;a++;l++;}
    	//             ans=ans+(a+1)*(b+1);
    	//             //if(i==n)break;
    	//             freq[s[l]]=0;d--;l++;
    	//             i--;i=c;
    	//         }
    	        
    	//     }
    	//     else freq[s[i]]++;
    	// }return ans;
    }
};

*/
39773