diff --git a/2537. Count the Number of Good Subarrays b/2537. Count the Number of Good Subarrays new file mode 100644 index 0000000..46b4d6f --- /dev/null +++ b/2537. Count the Number of Good Subarrays @@ -0,0 +1,25 @@ +class Solution { +public: + long long countGood(vector& nums, int k) { + long long int l = 0, r= 0; + long long int ans = 0, count = 0; + unordered_map mp; + int n = nums.size(); + while(r < n){ + mp[nums[r]]++; + count += (mp[nums[r]]-1); + while(l < n && (count- (mp[nums[l]]-1)) >= k){ + mp[nums[l]]--; + count -= mp[nums[l]]; + l+=1; + + } + if(count >= k ){ + ans += (l+1); + } + r++; + + } + return ans; + } +};