The Perfect Cow
InterviewTime limit1sMemory limit128 MB
Given an odd N by N grid, take the median of each row, then return the median of those N row medians.
- Level
Easy3 of 10
- Topics
- Sorting, Array, Implementation
- Solved
- No attempts yet
Problem
For yet another year, Farmer John was named "Dairy Farmer of the Year". He wants to exhibit his most perfect cow at the upcoming Cow Convention.
According to the scientific literature, beauty is a trend toward the average rather than the existence of some superior trait. So Farmer John wants to find his most average cow and bring her to the convention.
Each cow has her beauty rating () inscribed on a tag in her ear. Farmer John lines the cows up into an square (, and is odd).
He first finds the median cow in each row. The median cow is the one whose beauty rating is greater than or equal to half of the cows in her row and also less than or equal to half of them — that is, the middle value when the row is sorted. From those medians he then takes the median again, and that value is the beauty rating of the most perfect (average) cow.
Given the cows, find the beauty rating of the most perfect cow.
Input
- Line 1: A single integer .
- Lines 2..N+1: Line contains space-separated integers, the beauty ratings for row of the cow square.
Output
- Line 1: A single integer, the beauty rating of the most perfect cow as described above.
Hint
In the example, the medians of the rows are, respectively, and . The median of those five values is .