> For the complete documentation index, see [llms.txt](https://luj.gitbook.io/code/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://luj.gitbook.io/code/two-pointers/sliding-windows/subarrays-with-k-different-integers.md).

# Subarrays with K Different Integers

Given an array`A`of positive integers, call a (contiguous, not necessarily distinct) subarray of`A`\_good\_if the number of different integers in that subarray is exactly`K`.

(For example,`[1,2,3,1,2]`has`3`different integers:`1`,`2`, and`3`.)

Return the number of good subarrays of`A`.

## Example

**Example 1:**

```
Input: 
A = [1,2,1,2,3], K = 2

Output: 
7
Explanation: 
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].
```

**Example 2:**

```
Input: 
A = [1,2,1,3,4], K = 3

Output: 3
Explanation: 
Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
```

## Note

关键是求刚好K个不同的，参考之前的最多k个不同

```
numAtMostK(nums, k) - numAtMostK(nums, k - 1);
```

子串最多有k个不同的，满足条件的最大长度的窗口长度为就为个数

## Code

```java
class Solution {
    public int subarraysWithKDistinct(int[] nums, int k) {
        return numAtMostK(nums, k) - numAtMostK(nums, k - 1);
    }

    private int numAtMostK(int[] nums, int k) {
        Map<Integer, Integer> map = new HashMap<>();
        int j = 0;
        int len = nums.length;
        int count = 0;
        int res = 0;
        for (int i = 0; i < len; i++) {
            while (j < len && count <= k) {
                map.put(nums[j], map.getOrDefault(nums[j], 0) + 1);
                if (map.get(nums[j]) == 1) {
                    if (count == k) {
                        map.put(nums[j], map.get(nums[j]) - 1);
                        break;
                    }
                    count++;
                }
                j++;
            }
            res += j - i;
            if (map.getOrDefault(nums[i], 0) == 1) {
                count--;
            }
            map.put(nums[i], map.get(nums[i]) - 1);   
        }

        return res;
    }
}
```
