C# Solution for Subarrays With K Different Integers Problem
Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition behind this C# solution is similar to the Java solution: to use a sliding
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind this C# solution is similar to the Java solution: to use a sliding window approach to count the number of subarrays with at most k distinct integers and then subtract the count of subarrays with at most (k - 1) distinct integers to get the count of subarrays with exactly k distinct integers.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1.\tUtilize a sliding window technique with two pointers (left and right) to iterate through the array.\n2.\tMaintain a dictionary to keep track of the frequency of each element within the current window.\n3.\tExpand the window by moving the right pointer and update the frequency map accordingly.\n4.\tShrink the window by moving the left pointer if the number of distinct integers exceeds k.\n5.\tCount the number of subarrays with at most k distinct integers by summing up the lengths of all valid subarrays.\n6.\tReturn the difference between the counts obtained for k and (k - 1).\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe time complexity of this solution is O(n), where n is the length of the input array. Both left and right pointers traverse the array only once.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity is also O(n). The dictionary can store at most n elements, where n is the length of the input array.\n\n# Code\n```\npublic class Solution {\n    public int SubarraysWithKDistinct(int[] nums, int k) {\n        return AtMostKDistinct(nums, k) - AtMostKDistinct(nums, k - 1);\n    }\n    private int AtMostKDistinct(int[] nums, int k) {\n        int count = 0;\n        Dictionary<int, int> frequencyMap = new Dictionary<int, int>();\n        int left = 0;\n        \n        for (int right = 0; right < nums.Length; right++) {\n            int num = nums[right];\n            if (!frequencyMap.ContainsKey(num))\n                frequencyMap[num] = 0;\n            frequencyMap[num]++;\n            \n            while (frequencyMap.Count > k) {\n                frequencyMap[nums[left]]--;\n                if (frequencyMap[nums[left]] == 0)\n                    frequencyMap.Remove(nums[left]);\n                left++;\n            }\n            \n            count += right - left + 1;\n        }\n        \n        return count;\n    }\n}\n```
['C#']