From be8d5d7a38434437d8488eca517fd5f7be9684e9 Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Wed, 16 Apr 2025 14:20:06 +0530 Subject: [PATCH] Create 2537. Count the Number of Good Subarrays --- 2537. Count the Number of Good Subarrays | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 2537. Count the Number of Good Subarrays 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; + } +};