Posts

Showing posts with the label sliding window

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 ...

Count Subarrays Between Min and Max

Count Fixed-Bound Subarrays — Satisfying MinK and MaxK Problem Statement You are given an array of integers nums and two integers minK and maxK . A fixed-bound subarray of nums is a subarray that satisfies the following conditions: The minimum value in the subarray is equal to minK . The maximum value in the subarray is equal to maxK . Your task is to return the number of fixed-bound subarrays in nums . Example 1: Input : nums = [1, 3, 5, 2, 7, 5], minK = 1, maxK = 5 Output : 2 Explanation : The fixed-bound subarrays are [1, 3, 5] and [1, 3, 5, 2] . Best Technique – Sliding Window + Boundary Tracking This problem can be tricky because you need to ensure the subarray contains both the minimum and maximum values exactly. We can efficiently solve this using a sliding window technique, maintaining boundaries to ensure that the subarrays stay valid. Different Approaches: Approach 1: Brute Force with Set Tracking We could try every possible subarray and check if it m...

Efficiently Count Complete Subarrays

Count Complete Subarrays — Match Distinct Count in Subarrays Problem Statement You’re given an array of positive integers nums . A complete subarray is defined as a subarray that contains all the distinct elements present in the entire array. Your task is to count the number of complete subarrays in nums . Explanation: The total number of distinct elements in the array is 3: {1, 2, 3}. Now, let’s find all subarrays that also contain all 3: [1,3,1,2] [1,3,1,2,2] [3,1,2] [3,1,2,2] So, there are 4 complete subarrays . Best Technique – Sliding Window + At-Most K Trick This problem might seem like just a subarray count, but it tests your ability to: Recognize a fixed-size condition inside variable-sized subarrays Use a hash map or frequency counter for distinct element tracking Leverage the at-most-k sliding window pattern Let’s explore multiple ways to solve this—from brute force to optimal—and see what each one teaches us. Different Approaches: ...