Exactly K Distinct — Solution & Editorial
'Exactly K' = 'at most K' − 'at most K−1', each computed by a sliding window that counts, for every right endpoint, the number of valid left endpoints.
Complexity: O(N)
Watch out: The count reaches ~5×109 — 64-bit.