Count Number of Nice Subarrays LeetCode solution in C++
1class Solution {2public:3vector<int> nums;4int solve(int k) {5int low = 0, high = 0, cnt = 0, res = 0;67while (high < nums.size()) {8if (nums[high] & 1) {9cnt++;1011while (cnt > k) {12if (nums[low] & 1) cnt--;13low++;14}15}16high++;17res += high - low;18}1920return res;21}22int numberOfSubarrays(vector<int> &nums, int k) {23this->nums = nums;24return solve(k) - solve(k - 1);25}26};