Compile Ready
Module 5 · Advanced Sliding Window

Count Number of Nice Subarrays

MediumProblem 13 of 17 9 min read ~22 min to solve LeetCode
Sliding WindowArrayCountingParityAt Most Trick
Asked atAmazonGoogleMicrosoftMetaOracle

Problem Statement

Given an integer array nums and an integer k, return the number of contiguous subarrays that contain exactly k odd numbers. Such subarrays are called nice subarrays.

Input

An integer array nums and an integer k, the exact number of odd values required in each valid subarray.

Output

An integer: the number of contiguous subarrays containing exactly k odd numbers.

Constraints

  • 1 <= nums.length <= 5 * 10^4
  • 1 <= nums[i] <= 10^5
  • 1 <= k <= nums.length

Examples

Example 1

Input:
nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The nice subarrays are **[1,1,2,1]** and **[1,2,1,1]**.

Example 2

Input:
nums = [2,4,6], k = 1
Output: 0
Explanation: There are no odd numbers, so no subarray can contain exactly one odd value.

Example 3

Input:
nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Explanation: The two odd values can be surrounded by any of the four even-prefix choices and four even-suffix choices, giving **4 * 4 = 16**.

Learning Objectives

  • Map parity to a binary signal where odd values behave like **1** and even values behave like **0**.
  • Reuse **exactly(K) = atMost(K) - atMost(K - 1)** for exact odd-count subarrays.
  • Maintain a window with at most **k** odd values using two pointers.
  • Connect this problem directly to Binary Subarrays With Sum.

Intuition

Pattern Identification

Ignore the actual magnitudes. The problem only cares whether each number is odd. That means the array can be viewed as a binary sequence: odd maps to 1, even maps to 0. Now the task is to count subarrays with binary sum exactly k.

As in the previous problem, exact counts are easier through the at-most difference. A window with at most k odd values is monotone under removing elements from the left. Therefore exactly(K) = atMost(K) - atMost(K - 1) counts exactly the nice subarrays without enumerating every start and end.

Common mistakes

  • ×Using the numeric sum of the array instead of counting odd values only.
  • ×Trying to reset the window at even numbers, even though evens can be part of many nice subarrays.
  • ×Forgetting that every valid at-most window ending at **right** contributes **right - left + 1** suffixes.
  • ×Handling **k - 1** without a negative guard in a reusable helper.

Algorithm Explanation

Window setup

Build atMost(limit) where the window state is oddCount, not a sum of values. When nums[right] is odd, increment oddCount. While oddCount > limit, move left forward and decrement oddCount whenever an odd value leaves.

Window visualization

For nums = [1,1,2,1,1] and limit = 3, when right = 3, the window [1,1,2,1] has exactly three odds and adds 4 valid suffixes. When right = 4 adds another odd, the window has four odds, so left moves past the first odd. The valid window becomes [1,2,1,1] and adds 4 suffixes for the at-most count.

Algorithm

  1. Treat each odd number as contributing 1 to oddCount and each even number as contributing 0.
  2. In atMost(limit), return 0 if limit < 0.
  3. Expand right and update oddCount when the entering number is odd.
  4. While oddCount > limit, remove nums[left] from the parity count and advance left.
  5. Add right - left + 1 to count all valid suffixes ending at right.
  6. Return atMost(k) - atMost(k - 1).

Solutions

Solution: At-most difference on odd count

This is Binary Subarrays With Sum after a parity transformation. The helper never needs to build a separate binary array; it counts odd values directly while maintaining the at-most invariant.

Step-by-step

  1. Call atMost(nums, k) to count subarrays with at most k odd numbers.
  2. Call atMost(nums, k - 1) to count subarrays with too few odd numbers.
  3. During each pass, increment oddCount when the right value is odd.
  4. Shrink from the left until oddCount is within the limit.
  5. Add right - left + 1 for each right edge and subtract the two pass totals.
Time

O(n)

Space

O(1)

Two linear passes with only pointers and an odd counter.

Java implementation

Loading…

Dry Run

Sample input

nums = [1,1,2,1,1], k = 3. Count atMost(3) and atMost(2) by tracking only odd values.

phaserightvalue parityleft after shrinkodd countsubarrays addedrunning countmeaning
atMost(3)0odd0111one odd is within limit
atMost(3)1odd0223two odds are within limit
atMost(3)2even0236even extends all suffixes
atMost(3)3odd03410limit reached
atMost(3)4odd13414shrink past the first odd
atMost(2)0odd0111one odd allowed
atMost(2)1odd0223limit reached
atMost(2)2even0236even does not change odd count
atMost(2)3odd1239remove the first odd
atMost(2)4odd22312remove the second odd

The helper counts are atMost(3) = 14 and atMost(2) = 12. The difference is 2, matching the two nice subarrays.

Interview Tips

State the transformation first: odd numbers are ones, even numbers are zeros. Then the problem becomes exact binary sum, so the same atMost(K) - atMost(K - 1) reasoning applies. This framing is stronger than presenting it as a brand-new trick and helps you generalize to other categorical-count windows.

Likely follow-ups

  • How would you solve this with prefix counts of odd numbers instead of sliding window?
  • How would you count subarrays with exactly **k** even numbers?
  • How would the solution change if the condition were exactly **k** values divisible by **3**?
  • How can you compute the answer by multiplying choices around the positions of odd values?

Similar Problems

Key Takeaways

  • Parity problems often reduce to binary-array problems.
  • Nice subarrays are subarrays with binary odd-count sum exactly **k**.
  • The at-most helper can count odd values directly without materializing a transformed array.
  • Even numbers are not separators; they multiply the number of valid starts and ends.
Reusable template: Map the property of interest to a binary count, then count exact-K subarrays by subtracting atMost(K - 1) from atMost(K).