2488. Count Subarrays With Median K Hard

@problem@discussion
#Array#Hash Table#Prefix Sum



1/**
2 * [2488] Count Subarrays With Median K
3 *
4 * You are given an array nums of size n consisting of distinct integers from 1 to n and a positive integer k.
5 * Return the number of non-empty subarrays in nums that have a median equal to k.
6 * Note:
7 * 
8 * 	The median of an array is the middle element after sorting the array in ascending order. If the array is of even length, the median is the left middle element.
9 * 	
10 * 		For example, the median of [2,3,1,4] is 2, and the median of [8,4,3,5,1] is 4.
11 * 	
12 * 	
13 * 	A subarray is a contiguous part of an array.
14 * 
15 *  
16 * <strong class="example">Example 1:
17 * 
18 * Input: nums = [3,2,1,4,5], k = 4
19 * Output: 3
20 * Explanation: The subarrays that have a median equal to 4 are: [4], [4,5] and [1,4,5].
21 * 
22 * <strong class="example">Example 2:
23 * 
24 * Input: nums = [2,3,1], k = 3
25 * Output: 1
26 * Explanation: [3] is the only subarray that has a median equal to 3.
27 * 
28 *  
29 * Constraints:
30 * 
31 * 	n == nums.length
32 * 	1 <= n <= 10^5
33 * 	1 <= nums[i], k <= n
34 * 	The integers in nums are distinct.
35 * 
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/count-subarrays-with-median-k/
40// discuss: https://leetcode.com/problems/count-subarrays-with-median-k/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45    pub fn count_subarrays(nums: Vec<i32>, k: i32) -> i32 {
46        0
47    }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_2488() {
58    }
59}
60


Back
© 2025 bowen.ge All Rights Reserved.