DSCPPAS262 - Editorial

The problem at hand is to determine the number of subarrays in a given array where the maximum element is exactly K. This problem, though seemingly simple, requires careful consideration of edge cases and efficient handling of the array to avoid unnecessary computations.

Problem Breakdown

We are given an array of integers, and our task is to count the subarrays where the maximum element is exactly K. To solve this problem, we can break it down into two key steps:

  1. Counting subarrays where the maximum element is less than or equal to K.
  2. Counting subarrays where the maximum element is less than or equal to K-1.

The difference between the two counts will give us the number of subarrays where the maximum element is exactly K.

Key Idea and Approach

To achieve this, we use a helper function totalSubarrays that counts all subarrays where the maximum element is not greater than a given value K. This function works as follows:

  1. Iterating through the array: We traverse the array and identify sequences of consecutive elements that are all less than or equal to K.
  2. Counting valid subarrays: For each such sequence, we calculate the number of subarrays that can be formed. If a sequence has count elements, the number of subarrays is given by the formula count * (count + 1) / 2. This formula derives from the fact that a sequence of n elements can form 1 + 2 + ... + n = n * (n + 1) / 2 subarrays.

Using this function, we calculate two values:

  • count1: The total number of subarrays where the maximum element is less than or equal to K-1.
  • count2: The total number of subarrays where the maximum element is less than or equal to K.

Finally, the difference count2 - count1 gives us the number of subarrays where the maximum element is exactly K.

Efficiency Consideration

The approach used in this solution is efficient with a time complexity of O(N), where N is the length of the array. This is because each element in the array is processed at most twice—once when counting valid subarrays and once when moving past elements greater than K. This makes the solution scalable for large arrays.

Edge Cases

  • All elements greater than K: The function handles this by skipping over such elements, resulting in zero valid subarrays.
  • All elements less than or equal to K-1: Here, count2 would be equal to count1, and thus the final count would correctly be zero, indicating no subarrays with a maximum element of exactly K.

Bonus

This problem can be also solved using the monotonic stack. This approach efficiently counts the number of subarrays where the maximum element is exactly K by utilizing the concepts of “Next Greater Element” (NGE) and “Previous Greater Element” (PGE). By first identifying the indices of the next and previous greater elements for each element in the array using stacks, we can determine the number of valid subarrays where each occurrence of K is the maximum. The total count is calculated by multiplying the number of elements between K and its nearest greater elements on both sides. This method runs in (O(N)) time and requires (O(N)) space, making it well-suited for handling large arrays efficiently.