Subarrays with K Different Integers

Minimum Window and Advanced Constraints DSA practice problem on Onlearn.

Difficulty: hard.

Topics: How do you find the number of subarrays with exactly k different integers?, Arrays, Frequency Counting, Hash Map, Sliding Window, Two Pointers, Time Complexity, Space Complexity, Big O Notation, Algorithm, sliding window, subarray, array properties, combinatorics, frequency counting, Subarray/Substring Problems, Combinatorics & Binomial Coefficients.

Subarray with K Different Integers A (contiguous) subarray is considered good if the number of different integers in that subarray is exactly k. Your task is to find the total count of all such good subarrays in a given array A of positive integers. Input Specification: The first line contains an integer n (the size of array A). The second line contains n space separated integers, representing the elements of array A. The third line contains an integer k. Output Specification: An integer representing the total count of good subarrays. Constraints: 1 <= n <= 20000 1 <= A[i] <= n 1 <= k <= n Sample Test Case: Input: Output: Explanation: The subarrays with exactly 2 distinct integers are: [1,2] [2,1] [1,2] [2,3] [1,2,1] [2,1,2] [1,2,1,2] Total: 7