Above the Median
InterviewTime limit1sMemory limit128 MB
Given N cow heights, count contiguous ranges whose defined median (the ceil(K/2)-th smallest) is at least a threshold X.
- Level
Medium6 of 10
- Topics
- Prefix sum, Binary search, Sorting, Array
- Solved
- No attempts yet
Problem
Farmer John has lined up his () cows in a row to measure their heights; cow has height () nanometers—FJ believes in precise measurements! He wants to take a picture of some contiguous range of the cows to submit to a bovine photography contest at the county fair.
The contest has an unusual rule: a photograph may be submitted only if the cows it shows have a median height of at least a threshold ().
For this problem, the median of an array (which has elements) is defined as after is sorted in non-decreasing order, where is rounded up (or itself when it is already an integer). For example, the median of is , and the median of is .
Count the number of distinct contiguous ranges of cows that FJ could submit to the contest.
Input
- Line 1: Two space-separated integers and .
- Lines 2 to : Line contains the single integer .
Output
- Line 1: The number of contiguous ranges whose median is at least . This value may not fit in a 32-bit integer.