|
| 1 | +package com.thealgorithms.slidingwindow; |
| 2 | + |
| 3 | +/** |
| 4 | + * The Subarrays with K Different Integers algorithm counts the number of subarrays |
| 5 | + * that contain exactly k distinct integers. |
| 6 | + * |
| 7 | + * <p> |
| 8 | + * Worst-case performance O(n) |
| 9 | + * Best-case performance O(n) |
| 10 | + * Average performance O(n) |
| 11 | + * Worst-case space complexity O(k) |
| 12 | + * |
| 13 | + * @author https://github.com/Chiefpatwal |
| 14 | + */ |
| 15 | +public final class SubarraysWithKDifferentIntegers { |
| 16 | + |
| 17 | + // Prevent instantiation |
| 18 | + private SubarraysWithKDifferentIntegers() { |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * This method counts the number of subarrays with exactly k different integers. |
| 23 | + * |
| 24 | + * @param arr is the input array |
| 25 | + * @param k is the number of distinct integers |
| 26 | + * @return the count of subarrays with exactly k distinct integers |
| 27 | + */ |
| 28 | + public static int subarraysWithKDistinct(int[] arr, int k) { |
| 29 | + return atMostKDistinct(arr, k) - atMostKDistinct(arr, k - 1); |
| 30 | + } |
| 31 | + |
| 32 | + // Helper method to count subarrays with at most k distinct integers |
| 33 | + private static int atMostKDistinct(int[] arr, int k) { |
| 34 | + if (k <= 0) return 0; |
| 35 | + |
| 36 | + int count = 0; // To store the count of valid subarrays |
| 37 | + int left = 0; // Left index of the sliding window |
| 38 | + int[] frequency = new int[arr.length + 1]; // Frequency array to count distinct integers |
| 39 | + |
| 40 | + for (int right = 0; right < arr.length; right++) { |
| 41 | + if (frequency[arr[right]] == 0) { |
| 42 | + k--; // New distinct integer added |
| 43 | + } |
| 44 | + frequency[arr[right]]++; // Increment the frequency of the current element |
| 45 | + |
| 46 | + while (k < 0) { // More than k distinct integers |
| 47 | + frequency[arr[left]]--; // Remove the leftmost element from the window |
| 48 | + if (frequency[arr[left]] == 0) { |
| 49 | + k++; // Distinct integer count reduced |
| 50 | + } |
| 51 | + left++; // Move the left index to the right |
| 52 | + } |
| 53 | + count += right - left + 1; // Count the number of valid subarrays ending at 'right' |
| 54 | + } |
| 55 | + |
| 56 | + return count; // Return the total count |
| 57 | + } |
| 58 | +} |
0 commit comments