 All Problems
Count Number of Nice Subarrays
medium
sliding window
prefix sum
arrays
amazon
google

Given an array of integers nums and an integer k, return the number of nice subarrays. A subarray is nice if it contains exactly k odd numbers.

Example 1:

Input: 1 1 2 1 1
k: 3
Output: 2
Explanation: [1,1,2,1] and [1,2,1,1].

Example 2:

Input: 2 4 6
k: 1
Output: 0

Constraints:

  • 1 ≤ nums.length ≤ 50000
  • 1 ≤ nums[i] ≤ 10⁵
  • 1 ≤ k ≤ nums.length

Input format: First line: space-separated array. Second line: k.

Output format: Count of nice subarrays.

Run to check your code against the sample cases, or submit to run every case