Sprinklr | OA | Find the number of subarrays which have median 'M'
Anonymous User
1080

Given an Array 'A' of size 'N', find the number of Subarrays which have median equal to 'M'. Median of the array is defined as :

  • If 'N' is odd then the median is middle of the sorted array
  • If 'N' is even then the median is left of two middle values of the sorted array.
Input - [2, 1, 3, 5, 4] , N = 5 , M = 2
Output - 3
Explanation : The subarrays are [2], [2,1,3] and [2,1,3,5] hence count is 3.

Note - Count of 'M' can be more than 1 in the array.
Can anyone solve it in O(n)?

Comments (2)