Count Subarrays With Median K - Problem

You are given an array nums of size n consisting of distinct integers from 1 to n and a positive integer k.

Return the number of non-empty subarrays in nums that have a median equal to k.

Note:

  • 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.
  • For example, the median of [2,3,1,4] is 2, and the median of [8,4,3,5,1] is 4.
  • A subarray is a contiguous part of an array.

Input & Output

Example 1 — Basic Case
$ Input: nums = [3,2,1], k = 2
Output: 3
💡 Note: Three subarrays have median 2: [2] (median=2), [3,2] (sorted=[2,3], median=2), [2,1] (sorted=[1,2], median=1... wait, let me recalculate. [2] median=2, [3,2] sorted=[2,3] median=2, [2,1] sorted=[1,2] median=1. Actually [3,2,1] sorted=[1,2,3] median=2. So subarrays: [2], [3,2], [3,2,1] = 3 total.
Example 2 — Single Element
$ Input: nums = [2,1,3], k = 1
Output: 3
💡 Note: Three subarrays have median 1: [1] (median=1), [2,1] (sorted=[1,2], median=1), and [1,3] (sorted=[1,3], median=1).
Example 3 — Multiple Valid
$ Input: nums = [1,3,2,4], k = 3
Output: 2
💡 Note: Two subarrays have median 3: [3] (median=3) and [1,3,2] (sorted=[1,2,3], left-middle median=2... wait, that's wrong. Let me recalculate: [3] median=3, [1,3,2] sorted=[1,2,3] median=2 (left-middle), [3,2] sorted=[2,3] median=2, [1,3,2,4] sorted=[1,2,3,4] median=2. Actually only [3] and [3,2,4] work... let me be more careful.

Constraints

  • n == nums.length
  • 1 ≤ n ≤ 105
  • 1 ≤ nums[i], k ≤ n
  • All integers in nums are distinct

Visualization

Tap to expand
Count Subarrays With Median K INPUT Array nums: 3 idx 0 2 idx 1 1 idx 2 Target Median k: k = 2 Find subarrays where median equals k All Subarrays: [3], [2], [1], [3,2], [2,1], [3,2,1] n = 3, distinct 1 to n ALGORITHM STEPS 1 Find k's position k=2 is at index 1 2 Balance Transform num > k: +1, num < k: -1 nums: [3, 2, 1] balance: [+1, 0, -1] 3>2: +1, 2=2: 0, 1<2: -1 3 HashMap Count Track prefix sums left of k Left of k: prefix[0]=0, [1]=+1 map: {0:1, 1:1} 4 Count Valid Pairs sum=0 or sum=-1 is valid Odd: bal=0, Even: bal=-1 Right: [2]=0, [2,1]=-1 Match: 1+1+1=3 pairs FINAL RESULT Valid Subarrays: [2] median = 2 OK [3, 2] sorted [2,3] med=2 OK [2, 1] sorted [1,2] med=2 OK Output: 3 3 subarrays have median equal to k=2 Time: O(n), Space: O(n) Key Insight: Balance Transformation with Hash Map Transform elements relative to k: greater than k becomes +1, less than k becomes -1, k itself is 0. A subarray containing k has median k if balance is 0 (odd length) or -1 (even length, left middle). Use prefix sum and hash map to count pairs: left_sum + right_sum = 0 or -1 for valid subarrays. TutorialsPoint - Count Subarrays With Median K | Balance Transformation with Hash Map --> -->
Asked in
Google 15 Facebook 12 Amazon 8
23.4K Views
Medium Frequency
~30 min Avg. Time
867 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen