#K46682. Sublist Median Calculation
Sublist Median Calculation
Sublist Median Calculation
You are given a list of integers and two indices start and end. Your task is to compute the median of the sublist defined by the range [start, end), i.e. starting from index start (inclusive) and ending at index end (exclusive).
The median is defined as follows:
- If the sublist has an odd number of elements, the median is the middle element after sorting the sublist.
- If the sublist has an even number of elements, the median is the average of the two middle numbers after sorting the sublist.
In mathematical notation, let \( L \) be the sorted sublist of length \( n \). Then the median \( M \) is given by:
\( M = \begin{cases} L_{\frac{n+1}{2}}, & \text{if } n \text{ is odd}\\ \frac{L_{\frac{n}{2}} + L_{\frac{n}{2}+1}}{2}, & \text{if } n \text{ is even} \end{cases} \)
Note: In this problem, indices are 0-based and the end
index is exclusive.
inputFormat
The input is read from standard input (stdin) and consists of three lines:
- The first line contains an integer \( n \), the number of elements in the list.
- The second line contains \( n \) space-separated integers representing the list.
- The third line contains two space-separated integers, \( start \) and \( end \), which define the sublist [start, end) for which you are to compute the median.
outputFormat
Output a single line to standard output (stdout) containing the median of the specified sublist as a floating point number. Even when the median is an integer, it should be printed in floating-point format (for example, 15.0).
## sample8
1 3 4 2 7 5 8 6
1 5
3.5
</p>