Count Subarrays with Max Element ≥ k Times
Max Frequency Subarray Problem – Number of Subarrays Where Maximum Appears at Least k Times Problem Statement: You are given an integer array nums[] and a positive integer k . A subarray is a contiguous portion of the array. Your task is to count how many subarrays contain the maximum element of the entire array at least k times . Example 1: Input: nums = [ 1 , 3 , 2 , 3 , 3 ], k = 2 Output: 6 Explanation: Subarrays where the max element (3) appears at least 2 times: [1, 3, 2, 3] [1, 3, 2, 3, 3] [3, 2, 3] [3, 2, 3, 3] [2, 3, 3] [3, 3] Best Technique – Two Pointers (Sliding Window) This problem is efficiently solved using the Two Pointers technique. We maintain a sliding window [left...right] and track how many times the maximum value appears within that window. As soon as it appears at least k times, we know that: All subarrays ending at right and starting at any position from left to right are valid! We use this to add (n - right) valid ...