<p>You are given an integer array <code>nums</code> and two integers <code>minK</code> and <code>maxK</code>.</p>

<p>A <strong>fixed-bound subarray</strong> of <code>nums</code> is a subarray that satisfies the following conditions:</p>

<ul>
	<li>The <strong>minimum</strong> value in the subarray is equal to <code>minK</code>.</li>
	<li>The <strong>maximum</strong> value in the subarray is equal to <code>maxK</code>.</li>
</ul>

<p>Return <em>the <strong>number</strong> of fixed-bound subarrays</em>.</p>

<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,3,5,2,7,5], minK = 1, maxK = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> The fixed-bound subarrays are [1,3,5] and [1,3,5,2].
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,1,1,1], minK = 1, maxK = 1
<strong>Output:</strong> 10
<strong>Explanation:</strong> Every subarray of nums is a fixed-bound subarray. There are 10 possible subarrays.
</pre>

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

<ul>
	<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i], minK, maxK &lt;= 10<sup>6</sup></code></li>
</ul>

class Solution {
public:
    long long countSubarrays(vector<int>& nums, int minK, int maxK) {
        long long i = 0, ans = 0;
        long long n = nums.size();
        while (i < n) {

            // Skip if that element is not in given range
            if (nums[i] < minK || nums[i] > maxK) {
                i++;
                continue;
            }
            // Step 1 : Find Range of numbers contains only elements from minK to maxK
            int j = i;
            while (j < n && nums[j] >= minK && nums[j] <= maxK) {
                j++;
            }


            //Step 2 : Now you have a range i to j - 1 containg valid elems
            // Maintain two counters for minElemK and maxElemK
            long long k = i;
            long long cntmn = 0, cntmx = 0;
            while (k < j) {

                // Note -> you have a range i to k to j
                // So lets i to k have both minK and maxK
                // So ans will be increase by j - k + 1 considering i is fixed
                // because subarrays are i..k , i..k+1,i...k+2 ... , i + j - 1 


                if (cntmn > 0 && cntmx > 0) {
                    ans += (j - k + 1);
                    // contracting Sliding window
                    if (nums[i] == minK)
                        cntmn--;
                    if (nums[i] == maxK)
                        cntmx--;
                    i++;
                } else {
                    // Expanding window
                    if (nums[k] == minK)
                        cntmn++;
                    if (nums[k] == maxK)
                        cntmx++;
                    k++;
                }
            }
            
            
            // Step 3 : At last k reach to j 
            // Just contract till condition satisfy 
            // Increase ans by (j - k + 1) as k = j just ans++
            while (i < j && (cntmx > 0 && cntmn > 0)) {
                ans++;
                if (nums[i] == minK)
                    cntmn--;
                if (nums[i] == maxK)
                    cntmx--;
                i++;
            };

            // 
            i = j;
        }
        return ans;
    }
};
82600