Problem Statement
Recall that the median of a group of numbers is the one in the middle when the numbers are listed in non-decreasing order. Finding the median of a large data set can be expensive, but we can approximate the true median quite cheaply by finding the medians of several small samples and then finding the median of those medians.
Consider a 3-by-3 matrix of digits, where each digit is a sample from some larger data set. Find the median of medians by first calculating the median of each row of the matrix, and then calculating the median of the three medians. For example, if the matrix were
724 681 331then the medians of the three rows would be 4, 6, and 3, respectively. The median of these three values is 4.
Create a class MedianOfMedians with a method median that takes a 3-by-3
matrix of digits as a
Definition
- Class:
- MedianOfMedians
- Method:
- median
- Parameters:
- String[]
- Returns:
- int
- Method signature:
- int median(String[] matrix)
- (be sure your method is public)
Constraints
- matrix contains exactly three elements.
- Each element of matrix contains exactly three characters, each of which is a digit ('0'-'9').
Examples
{ "724", "681", "331" }
Returns: 4
The example above.
{ "777", "777", "777" }
Returns: 7
{ "123", "456", "789" }
Returns: 5
{ "987", "321", "654" }
Returns: 5
{ "545", "565", "066" }
Returns: 5
{ "091", "807", "066" }
Returns: 6
{ "314", "159", "265" }
Returns: 5
{ "271", "828", "182" }
Returns: 2
{ "141", "421", "356" }
Returns: 2
{ "134", "217", "728" }
Returns: 3
{ "001", "000", "099" }
Returns: 0
{ "196", "709", "269" }
Returns: 6
{ "000", "099", "999" }
Returns: 9
{ "987", "876", "765" }
Returns: 7
{ "845", "938", "098" }
Returns: 8
{ "734", "129", "091" }
Returns: 2
{ "901", "823", "567" }
Returns: 3
{ "444", "000", "888" }
Returns: 4
{ "358", "979", "323" }
Returns: 5
{ "846", "264", "338" }
Returns: 4
{ "070", "106", "781" }
Returns: 1
{ "097", "720", "008" }
Returns: 2
{ "100", "010", "100" }
Returns: 0
{ "129", "222", "222" }
Returns: 2
{ "555", "555", "999" }
Returns: 5
{ "461", "123", "456" }
Returns: 4
{ "542", "654", "321" }
Returns: 4
{ "556", "556", "556" }
Returns: 5
{ "754", "788", "169" }
Returns: 6