From aee8ffa2ad44458b4bd5ced497b03560144c5c10 Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Fri, 29 Mar 2024 21:33:58 +0530 Subject: [PATCH] Create 2962. Count Subarrays Where Max Element Appears at Least K Times --- ...Where Max Element Appears at Least K Times | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 2962. Count Subarrays Where Max Element Appears at Least K Times 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; + } +};