Class Solution
java.lang.Object
g2401_2500.s2488_count_subarrays_with_median_k.Solution
2488 - Count Subarrays With Median K.<p>Hard</p>
<p>You are given an array <code>nums</code> of size <code>n</code> consisting of <strong>distinct</strong> integers from <code>1</code> to <code>n</code> and a positive integer <code>k</code>.</p>
<p>Return <em>the number of non-empty subarrays in</em> <code>nums</code> <em>that have a <strong>median</strong> equal to</em> <code>k</code>.</p>
<p><strong>Note</strong>:</p>
<ul>
<li>The median of an array is the <strong>middle</strong> element after sorting the array in <strong>ascending</strong> order. If the array is of even length, the median is the <strong>left</strong> middle element.
<ul>
<li>For example, the median of <code>[2,3,1,4]</code> is <code>2</code>, and the median of <code>[8,4,3,5,1]</code> is <code>4</code>.</li>
</ul>
</li>
<li>A subarray is a contiguous part of an array.</li>
</ul>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> nums = [3,2,1,4,5], k = 4</p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong> The subarrays that have a median equal to 4 are: [4], [4,5] and [1,4,5].</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> nums = [2,3,1], k = 3</p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong> [3] is the only subarray that has a median equal to 3.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], k <= n</code></li>
<li>The integers in <code>nums</code> are distinct.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
countSubarrays
public int countSubarrays(int[] nums, int k)
-