<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) {
        int n = nums.size();
        vector<int> sr(n,-1), lr(n,-1);
        vector<int> freq(n+1,0);
        int kuniq = 0;
        int cr = -1, cl = 0;
        // freq[nums[0]]=1;
        bool loop=1;

        while(cl<n && loop){

            while(kuniq<k){
                cr++;
                if(cr==n){
                    loop=0;
                    break;
                }
                if(freq[nums[cr]]==0){
                    kuniq+=1;
                }
                freq[nums[cr]]++;
            }
            if(!loop){
                break;
            }
            if(kuniq==k) sr[cl]=cr;

            freq[nums[cl]]--;
            if(freq[nums[cl]]==0){
                kuniq-=1;
            }
            cl+=1;
        }

        freq = vector<int> (n+1,0);
        kuniq = 0;
        cr = -1; cl = 0;
        loop=1;
        ;
        k++;

        while(cl<n && loop){

            while(kuniq<k){
                cr++;
                if(cr==n){
                    // kuniq+=1;
                    cr=n-1;
                    loop=1;
                    break;
                }
                if(freq[nums[cr]]==0){
                    kuniq+=1;
                }
                freq[nums[cr]]++;
            }
            // cout << " k " << k << " " << kuniq << "\n";
            if(kuniq==k) lr[cl]=cr;
            if(kuniq==k-1 && cr==n-1) lr[cl]=cr+1;

            freq[nums[cl]]--;
            if(freq[nums[cl]]==0){
                kuniq-=1;
            }
            cl+=1;
        }
        
        // cout << "lr"
        int sum=0;
        for(int i=0;i<n;i++){
            sum+=lr[i]-sr[i];
            // cout << " lr " << lr[i] << " sr " << sr[i] << endl;
        }
        return sum;


    }
};
45313