Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    2488 - Count Subarrays With Median K.

    Hard

    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.

    • A subarray is a contiguous part of an array.

    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.

    Constraints:

    • n == nums.length

    • <code>1 <= n <= 10<sup>5</sup></code>

    • 1 &lt;= nums[i], k &lt;= n

    • The integers in nums are distinct.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Integer countSubarrays(IntArray nums, Integer k)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait