diff --git a/2962. Count Subarrays Where Max Element Appears at Least K Times b/2962. Count Subarrays Where Max Element Appears at Least K Times new file mode 100644 index 0000000..3ead4fd --- /dev/null +++ b/2962. Count Subarrays Where Max Element Appears at Least K Times @@ -0,0 +1,26 @@ +class Solution { +public: + long long countSubarrays(vector& nums, int k) { + int n=nums.size(); + + int maxe=*max_element(nums.begin(), nums.end()); + + long long ans=0; + + int count=0; // keep track of count of maxElement in [i,j] + int i=0, j=0; + while(j=k){ + ans += (n-j); + + count -= (nums[i] == maxe); // shrink window + i++; + } + j++; + } + + return ans; + } +};