The Chosen Sub Matrix
InterviewTime limit1sMemory limit128 MB
For each N x N matrix, find the M x M sub matrix with the fewest distinct values, breaking ties by larger descending value lists, then by smaller top-left row and column.
- Level
Easy3 of 10
- Topics
- Brute force, Implementation, Matrix, Sorting
- Solved
- No attempts yet
Problem
From a given matrix, find the sub matrix that contains the fewest distinct values. When several sub matrices are tied on the number of distinct values, break the tie with the following rules:
- The sub matrix with fewer distinct values wins.
- If still tied, list each sub matrix's distinct values in descending order and compare these lists element by element; at the first position where they differ, the sub matrix with the larger value wins.
- If the sets of distinct values are identical too, choose the one whose top-left cell has the smaller row index, and if the row index is also equal, the smaller column index.
Matrix indices start at 1.
For example, consider the following matrix:
3 9 9 9
3 9 9 2
3 9 9 2
2 5 5 2
There are four possible sub matrices.
S1 (top-left 1, 1) — distinct values {9, 3}
3 9 9
3 9 9
3 9 9
S2 (top-left 1, 2) — distinct values {9, 2}
9 9 9
9 9 2
9 9 2
S3 (top-left 2, 1) — distinct values {9, 5, 3, 2}
3 9 9
3 9 9
2 5 5
S4 (top-left 2, 2) — distinct values {9, 5, 2}
9 9 2
9 9 2
5 5 2
S1 and S2 each have 2 distinct values, fewer than S4 (3) and S3 (4). Comparing S1's list (9, 3) with S2's list (9, 2), the first values match and S1 is larger at the second, so S1 ranks above S2. The overall ranking is S1, S2, S4, S3, so the chosen sub matrix is S1.
Input
The input consists of one or more test cases and continues until end of file. The first line of each test case contains two integers (), the size of the matrix, and (), the size of the sub matrix to choose. The next lines each contain integers separated by single spaces, describing the matrix. Every element of the matrix is an integer from to inclusive.
Output
For each test case, print on a single line the top-left index (row and column, separated by a single space) of the chosen sub matrix.