1248. Count Number of Nice Subarrays LeetCode Solution

In this guide, you will get 1248. Count Number of Nice Subarrays LeetCode Solution with the best time and space complexity. The solution to Count Number of Nice Subarrays problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Count Number of Nice Subarrays solution in C++
  4. Count Number of Nice Subarrays solution in Java
  5. Count Number of Nice Subarrays solution in Python
  6. Additional Resources
1248. Count Number of Nice Subarrays LeetCode Solution image

Problem Statement of Count Number of Nice Subarrays

Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.

Example 1:

Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers 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 in the array.

Example 3:

Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16

Constraints:

1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length

Complexity Analysis

  • Time Complexity:
  • Space Complexity:

1248. Count Number of Nice Subarrays LeetCode Solution in C++

class Solution {
 public:
  int numberOfSubarrays(vector<int>& nums, int k) {
    return numberOfSubarraysAtMost(nums, k) -
           numberOfSubarraysAtMost(nums, k - 1);
  }

 private:
  int numberOfSubarraysAtMost(vector<int>& nums, int k) {
    int ans = 0;

    for (int l = 0, r = 0; r <= nums.size();)
      if (k >= 0) {
        ans += r - l;
        if (r == nums.size())
          break;
        if (nums[r] % 2 == 1)
          --k;
        ++r;
      } else {
        if (nums[l] % 2 == 1)
          ++k;
        ++l;
      }

    return ans;
  }
};
/* code provided by PROGIEZ */

1248. Count Number of Nice Subarrays LeetCode Solution in Java

class Solution {
  public int numberOfSubarrays(int[] nums, int k) {
    return numberOfSubarraysAtMost(nums, k) - numberOfSubarraysAtMost(nums, k - 1);
  }

  private int numberOfSubarraysAtMost(int[] nums, int k) {
    int ans = 0;

    for (int l = 0, r = 0; r <= nums.length;)
      if (k >= 0) {
        ans += r - l;
        if (r == nums.length)
          break;
        if (nums[r] % 2 == 1)
          --k;
        ++r;
      } else {
        if (nums[l] % 2 == 1)
          ++k;
        ++l;
      }

    return ans;
  }
}
// code provided by PROGIEZ

1248. Count Number of Nice Subarrays LeetCode Solution in Python

class Solution:
  def numberOfSubarrays(self, nums: list[int], k: int) -> int:
    def numberOfSubarraysAtMost(k: int) -> int:
      ans = 0
      l = 0
      r = 0

      while r <= len(nums):
        if k >= 0:
          ans += r - l
          if r == len(nums):
            break
          if nums[r] & 1:
            k -= 1
          r += 1
        else:
          if nums[l] & 1:
            k += 1
          l += 1
      return ans

    return numberOfSubarraysAtMost(k) - numberOfSubarraysAtMost(k - 1)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.