#2488
Count Subarrays With Median K
candidate master · 1485 · lc hard +32 · failed · 47.7% accepted · 655 likes · top 33%
Description
Given an array nums of size n containing distinct integers from 1 to n and a positive integer k, count how many non-empty subarrays have a median equal to k.
The median is the middle element of a sorted array; for even-length arrays, it is the left of the two middle elements (for example, the median of [2,3,1,4] is 2).
Example 1:
Input: nums = [3,2,1,4,5], k = 4
Output: 3
Explanation: The subarrays that have a median equal to 4 are: [4], [4,5] and [1,4,5].
Example 2:
Input: nums = [2,3,1], k = 3
Output: 1
Explanation: [3] is the only subarray that has a median equal to 3.
Code
1
2
3