Close Menu
    Code and Debug
    • Home
    • Our Courses
    • Blog
    • About Us
    • Contact Us
    Facebook X (Twitter) Instagram YouTube WhatsApp
    • Home
    • Our Courses
    • Blog
    • About Us
    • Contact Us
    Facebook Instagram YouTube LinkedIn WhatsApp
    Code and Debug – BlogCode and Debug – Blog
    Code and Debug – BlogCode and Debug – Blog
    Home»Data Structures & Algorithms»Count Number of Nice Subarrays | Leetcode 1248
    Data Structures & Algorithms

    Count Number of Nice Subarrays | Leetcode 1248

    Anirudh KhuranaBy Anirudh Khurana20 August 2025No Comments3 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Featured image of a question to solve Count Number of Nice Subarrays
    Share
    Facebook Twitter LinkedIn Pinterest Email

    The problem “Count Number of Nice Subarrays” asks: given an integer array nums and an integer k, count the number of contiguous subarrays that contain exactly k odd numbers. This is a perfect use case for the at-most trick: count subarrays with at most k odds, subtract subarrays with at most k−1 odds. The difference gives exactly k odds.

    Here’s the [Problem Link] to begin with.


    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

    Optimal Solution: Sliding Window (At-Most → Exact)

    Intuition and Approach

    • Maintain a window [left..right] and track Sum = number of odds in the window.
    • Expand right; add nums[right] % 2 to Sum.
    • While Sum > goal, shrink from the left, subtracting nums[left] % 2, and increment left.
    • For each right, after enforcing Sum ≤ goal, all subarrays ending at right and starting from any index in [left..right] are valid, contributing (right − left + 1) to the count.
    • Do this once for goal = k and once for goal = k−1; the difference is the number of subarrays with exactly k odds.

    Code

    class Solution:
        def countSubArrayLessThanOrEqualToGoal(self, nums, goal):
            if goal < 0:
                return 0
            count = 0
            n = len(nums)
            left = 0
            right = 0
            Sum = 0
            while right < n:
                # Add 1 if nums[right] is odd, else add 0
                Sum += nums[right] % 2
                # Shrink window until number of odds <= goal
                while Sum > goal:
                    Sum -= nums[left] % 2
                    left += 1
                # All subarrays ending at right and starting from [left..right] are valid
                count = count + ((right - left) + 1)
                right += 1
            return count
    
        def numberOfSubarrays(self, nums: List[int], k: int) -> int:
            return self.countSubArrayLessThanOrEqualToGoal(
                nums, k
            ) - self.countSubArrayLessThanOrEqualToGoal(nums, k - 1)

    Why This Works

    • The window always satisfies “number of odds ≤ goal.”
    • For each right, once the window is valid, every start position from left to right yields a valid subarray, hence (right − left + 1) contributions.
    • exact K = atMost(K) − atMost(K−1) is a standard identity that converts exact counting into two linear at-most counts.

    Complexity

    • Time: O(n) – each index is visited at most twice (once by right, once by left).
    • Space: O(1) – constant auxiliary variables.

    Tips and Edge Cases

    • If k = 0, answer counts subarrays with zero odd numbers (i.e., all-even subarrays); the formula still works because atMost(−1) returns 0 by guard.
    • The method is robust for any non-negative k and any integers in nums—only odd/even matters.
    • If you prefer prefix-sum + hashmap, you can map odds to 1, evens to 0, and count subarrays with sum k in O(n) time but O(n) space. The sliding-window at-most trick achieves O(1) space.

    Takeaway

    Transforming the array into oddness (0/1) and using the identity exact K = atMost(K) − atMost(K−1) lets you solve “Count Number of Nice Subarrays” in a clean, linear-time, constant-space manner—ideal for interviews and production.

    Join our Advance DSA COURSE

    For any changes to the article, kindly email at code@codeanddebug.in or contact us at +91-9712928220.

    Hard Sliding Window and Two Pointers
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleBinary Subarrays With Sum | Leetcode 930 | Optimal Prefix/Sliding Window Trick
    Next Article Number of Substrings Containing All Three Characters | Leetcode 1358 | Optimal Solution using Sliding Window
    Anirudh Khurana
    • Website

    Anirudh Khurana is the founder of Code and Debug, an Indian EdTech platform that helps students and working professionals crack coding interviews and move into data and AI roles. He teaches DSA, Python, SQL, data engineering and machine learning through live bootcamps and his YouTube channel, Code and Debug, which has over 50,000 subscribers. Every guide on this blog is written or reviewed by him and tested with real code.

    Related Posts

    Data Structures & Algorithms

    DSA Interview Cheat Sheet: All Patterns You Need to Know

    4 April 2026
    Data Structures & Algorithms

    Symmetric Tree | Leetcode 101 | Recursive DFS Approach

    2 September 2025
    Data Structures & Algorithms

    Binary Tree Right Side View | Leetcode 199 | BFS and DFS Approaches

    2 September 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Categories
    • AI & Machine Learning (4)
    • Data Structures & Algorithms (241)
      • Beginner (82)
      • Expert (52)
      • Intermediate (106)
    • Pandas (1)
    • Python (1)
    • Resume (1)
    • SQL (1)
    Recent Posts

    Confusion Matrix in Machine Learning: An Interactive Guide

    21 October 2026

    100 SQL Interview Questions with Queries and Answers (Freshers to Experienced)

    17 October 2026

    Logistic Regression in Machine Learning: Explained Visually (with Python)

    17 October 2026

    Linear Regression in Machine Learning: Explained Visually (with Python)

    17 October 2026

    100 Python Interview Questions and Answers for Freshers and Experienced (Free PDF)

    17 October 2026
    Facebook Instagram YouTube LinkedIn WhatsApp
    © 2025 Code and Debug. All rights reserved.

    Type above and press Enter to search. Press Esc to cancel.