Sprouts
Time limit3sMemory limit1024 MB
Find the largest gap between the median and the mean of sprout counts over every K x K block of an N x N grid, scaled by K squared.
- Level
Medium5 of 10
- Topics
- Sliding window, Prefix sum, Matrix
- Solved
- No attempts yet
Problem
Baku divided a large square plot of land into equal parts and made small square fields. He planted seeds in each field. One week later, some seeds had sprouted and others had not. The figure below shows fields with seeds planted in each. The top left field has 2 sprouts, and the bottom right field has 1 sprout.

Baku recently learned about the median. The median is the middle value when an odd number of given values is sorted. For example, if the five values are , the median is .
Baku wants to compare the median and the mean of the sprout counts. To test many cases, he compares the median and the mean over every block of adjacent fields. This gives comparisons for the fields.
For the figure above, the table below shows the medians and means for each block. The top left block has sprout counts , so its median is and its mean is . The top right block has median and mean .
Baku wants the maximum difference between the median and the mean. In the example above, the maximum occurs in the block marked in red in the figure below. There the median is and the mean is , so the difference is .
For convenience, Baku wants the maximum of the difference between the median and the mean multiplied by . In the example above, the answer is .
Function
You must implement the following function. It is called exactly once.
int diff( int K, int[][] V ) ;
is an array (vector). It gives the sprout count for each field. Compute the difference between the median and the mean for each block, multiply it by , and return the maximum value.
Constraints
- is odd and
- ()





