Skip to main content
LeetCode 992, Hard. Topics: Array, Hash Table, Sliding Window, Counting. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 12 parametrized pytest cases, and a playground notebook:

Problem

Given an integer array nums and an integer k, return the number of good subarrays of nums. A good array is an array where the number of different integers in that array is exactly k.
  • For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.
A subarray is a contiguous part of an array.

Examples

Constraints

  • 1 <= nums.length <= 2 * 10^4
  • 1 <= nums[i], k <= nums.length

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026