Skip to content

Commit b68d8bd

Browse files
committed
Add detailed comments and reference to CountNiceSubarrays
1 parent 9fb75ce commit b68d8bd

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

src/main/java/com/thealgorithms/slidingwindow/CountNiceSubarrays.java

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,96 @@
11
package com.thealgorithms.slidingwindow;
22

3-
public class CountNiceSubarrays {
3+
/**
4+
* Counts the number of "nice subarrays".
5+
* A nice subarray is a contiguous subarray that contains exactly k odd numbers.
6+
*
7+
* This implementation uses the sliding window technique.
8+
*
9+
* Reference:
10+
* https://leetcode.com/problems/count-number-of-nice-subarrays/
11+
*
12+
* Time Complexity: O(n)
13+
* Space Complexity: O(n)
14+
*/
15+
public final class CountNiceSubarrays {
16+
17+
// Private constructor to prevent instantiation
418
private CountNiceSubarrays() {
519
}
620

21+
/**
22+
* Returns the count of subarrays containing exactly k odd numbers.
23+
*
24+
* @param nums input array of integers
25+
* @param k number of odd elements required in the subarray
26+
* @return number of nice subarrays
27+
*/
728
public static int countNiceSubarrays(int[] nums, int k) {
29+
830
int n = nums.length;
31+
32+
// Left pointer of the sliding window
933
int left = 0;
34+
35+
// Tracks number of odd elements in the current window
1036
int oddCount = 0;
37+
38+
// Final answer: total number of nice subarrays
1139
int result = 0;
40+
41+
/*
42+
* memo[i] stores how many valid starting positions exist
43+
* when the left pointer is at index i.
44+
*
45+
* This avoids recomputing the same values again.
46+
*/
1247
int[] memo = new int[n];
1348

49+
// Right pointer moves forward to expand the window
1450
for (int right = 0; right < n; right++) {
51+
52+
// If current element is odd, increment odd count
1553
if ((nums[right] & 1) == 1) {
1654
oddCount++;
1755
}
1856

57+
/*
58+
* If oddCount exceeds k, shrink the window from the left
59+
* until oddCount becomes valid again.
60+
*/
1961
if (oddCount > k) {
2062
left += memo[left];
2163
oddCount--;
2264
}
2365

66+
/*
67+
* When the window contains exactly k odd numbers,
68+
* count all possible valid subarrays starting at `left`.
69+
*/
2470
if (oddCount == k) {
71+
72+
/*
73+
* If this left index hasn't been processed before,
74+
* count how many consecutive even numbers follow it.
75+
*/
2576
if (memo[left] == 0) {
2677
int count = 0;
2778
int temp = left;
79+
80+
// Count consecutive even numbers
2881
while ((nums[temp] & 1) == 0) {
2982
count++;
3083
temp++;
3184
}
85+
86+
/*
87+
* Number of valid subarrays starting at `left`
88+
* is (count of even numbers + 1)
89+
*/
3290
memo[left] = count + 1;
3391
}
92+
93+
// Add number of valid subarrays for this left position
3494
result += memo[left];
3595
}
3696
}

src/test/java/com/thealgorithms/slidingwindow/CountNiceSubarraysTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ void testSingleOdd() {
2424
@Test
2525
void testMultipleChoices() {
2626
int[] nums = {2, 2, 1, 2, 2, 1, 2};
27-
assertEquals(9, CountNiceSubarrays.countNiceSubarrays(nums, 2));
27+
assertEquals(6, CountNiceSubarrays.countNiceSubarrays(nums, 2));
2828
}
2929
}

0 commit comments

Comments
 (0)