Skip to content

Commit 4e87fe1

Browse files
committed
added new algo
1 parent 47f3c31 commit 4e87fe1

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.thealgorithms.slidingwindow;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
/**
8+
* Test cases for SubarraysWithKDifferentIntegers algorithm.
9+
*
10+
* <p>
11+
* This class provides test cases to verify the correctness of the subarraysWithKDistinct
12+
* method.
13+
*
14+
* @author https://github.com/Chiefpatwal
15+
*/
16+
public class SubarraysWithKDifferentIntegersTest {
17+
18+
@Test
19+
public void testEdgeCases() {
20+
// Test with an empty array
21+
assertEquals(0, SubarraysWithKDifferentIntegers.subarraysWithKDistinct(new int[] {}, 1));
22+
23+
// Test with k greater than the number of distinct elements
24+
assertEquals(0, SubarraysWithKDifferentIntegers.subarraysWithKDistinct(new int[] {1, 2, 3}, 5));
25+
26+
// Test with k = 0
27+
assertEquals(0, SubarraysWithKDifferentIntegers.subarraysWithKDistinct(new int[] {1, 2, 3}, 0));
28+
29+
// Test with single element arrays
30+
assertEquals(1, SubarraysWithKDifferentIntegers.subarraysWithKDistinct(new int[] {1}, 1));
31+
assertEquals(0, SubarraysWithKDifferentIntegers.subarraysWithKDistinct(new int[] {1}, 2));
32+
33+
// Test case where all elements are the same
34+
assertEquals(6, SubarraysWithKDifferentIntegers.subarraysWithKDistinct(new int[] {2, 2, 2}, 1)); // Corrected expected value
35+
assertEquals(0, SubarraysWithKDifferentIntegers.subarraysWithKDistinct(new int[] {2, 2, 2}, 2)); // Not enough distinct elements
36+
}
37+
}

0 commit comments

Comments
 (0)