<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p>

<p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,1,2,1,1], k = 3
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,4,6], k = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no odd numbers in the array.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,2,2,1,2,2,1,2,2,2], k = 2
<strong>Output:</strong> 16
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 50000</code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10^5</code></li>
	<li><code>1 &lt;= k &lt;= nums.length</code></li>
</ul>

// https://leetcode.com/problems/count-number-of-nice-subarrays/description/
// https://www.youtube.com/watch?v=JpXlsCAD1kg
#include <bits/stdc++.h>
using namespace std;

// Approach 1 : Brute force O(n^2)

// Approach 2 : Using hash map
// TC : O(n)
// SC : O(n)
class Solution1
{
public:
    int numberOfSubarrays(vector<int> &nums, int k)
    {
        // map to remember odd count if we have seen it in the past
        unordered_map<int, int> map_odd_count;
        int result = 0;
        int oddCount = 0;
        // We have seen oddCOunt = 0 once in past ( initially)
        map_odd_count[oddCount] = 1;
        int n = nums.size();
        for (int i = 0; i < n; i++)
        {
            oddCount += nums[i] % 2;

            if (map_odd_count[oddCount - k])
            {
                result += map_odd_count[oddCount - k];
            }

            map_odd_count[oddCount]++;
        }
        return result;
    }
};

// Approach 2 : Using subarray sum = k
// TC : O(n)
// SC : O(n)
// https://www.youtube.com/watch?v=GmCrhvnRi6k
class Solution
{
    int numSubarraysWithSum(vector<int> &nums, int goal)
    {
        unordered_map<int, int> map_seen_count; // [sum] -> count seen
        int result = 0;
        int prefixSum = 0;
        map_seen_count[prefixSum] = 1; // IMP : For map[0] = 1 ( 0 prefix sum we have initially)
        for (auto num : nums)
        {
            // update prefix sum
            prefixSum += num;

            int remaining_sum = prefixSum - goal;
            // Is remaining sum present in map seen  ?
            if (map_seen_count.find(remaining_sum) != end(map_seen_count))
            {
                // Yes, update result by seen count
                result += map_seen_count[remaining_sum];
            }

            // add prefix sum to map_seen
            map_seen_count[prefixSum]++;
        }
        return result;
    }

public:
    int numberOfSubarrays(vector<int> &nums, int k)
    {
        // Set odd to 1 and even to 0
        for (int i = 0; i < nums.size(); i++)
        {
            nums[i] = nums[i] % 2 ? 1 : 0;
        }
        // Call the function to find number of subarray with sum k
        return numSubarraysWithSum(nums,k);
    }
};
94545