> 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;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://luj.gitbook.io/code/two-pointers/sliding-windows/subarrays-with-k-different-integers.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
