#K72827. Distinct Submatrix Finder
Distinct Submatrix Finder
Distinct Submatrix Finder
Given a matrix \(A\) of size \(n \times m\) filled with positive integers and a positive integer \(k\), your task is to determine whether there exists a \(k \times k\) submatrix \(B\) in which every row and every column contains \(k\) distinct elements.
Formally, if the submatrix \(B\) is taken from \(A\) starting at row \(i\) and column \(j\) (with \(1 \leq i \leq n-k+1\) and \(1 \leq j \leq m-k+1\)), then for every \(0 \leq p, q < k\), the \(p\)-th row and the \(q\)-th column of \(B\) must have all distinct entries.
inputFormat
The first line of input contains three integers \(n\), \(m\), and \(k\) representing the number of rows, number of columns, and the size of the submatrix, respectively. Following this, there are \(n\) lines, each containing \(m\) space-separated integers representing the matrix \(A\).
outputFormat
Output a single line containing YES
if a valid \(k \times k\) submatrix exists, or NO
otherwise.
4 5 2
1 2 3 4 5
5 6 7 8 9
9 10 11 12 13
13 14 15 16 17
YES