Problem Description
Given an array nums containing distinct integers from 1 to n and a positive integer k, determine the number of non-empty contiguous subarrays in nums that have k as their median. The median is defined as the middle element after sorting a subarray in ascending order (if even-length, it is the left middle element). Note that for a subarray to have median k, it must include k.
Key Insights
- Transform the array by mapping each element to:
- 0 if it equals k
- 1 if it is greater than k
- -1 if it is less than k
- Find the index (pivot) where nums equals k. All valid subarrays must include this pivot.
- For subarrays to satisfy the median condition, the cumulative “balance” of 1’s and -1’s must be either 0 or -1 when combining segments to the left and right of the pivot.
- Use a prefix sum technique on the left segment of the pivot to count balance frequencies.
- For each element in the right segment (starting at the pivot), accumulate the balance and count complementary prefix frequencies that form a valid subarray.
Space and Time Complexity
Time Complexity: O(n) – We traverse the array twice (once for the left side and once for the right side). Space Complexity: O(n) – In the worst case, the hash table storing prefix sum frequencies could contain O(n) entries.
Solution
The approach involves transforming the array based on comparisons with k, then finding the pivot index where the element equals k. For all indices left of the pivot, accumulate a prefix “balance” by adding -1 when an element is less than k and +1 when greater than k. Store the frequency of these prefix sums in a dictionary. Next, starting at the pivot and moving rightwards, update a running balance using the same transformation. For each new balance, valid subarrays that include k can be found by adding the frequencies of the current balance and (current balance - 1) from the left side dictionary. This works because a valid subarray, after insertion of k, must achieve a balance that conforms with the median requirement (equal numbers of values higher and lower than k or one extra higher value for even lengths with left middle median). Finally, sum these counts to get the answer.