The Question
Coding
Subarrays with K Distinct Integers
Given an integer array and an integer k, implement an efficient algorithm to calculate the total number of subarrays that contain at most k distinct integers. Discuss the time and space complexity, and explain how this approach can be extended to find the number of subarrays with exactly k distinct integers.
Java
Sliding Window
Two Pointers
HashMap
March 8, 2026