Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a matrix of dimensions N x M.
Every cell has an integer.
Now, I want for every 'rectangle', to verify that all its corners are not the same.
This matrix is fine:
enter image description here
This matrix is not:
enter image description here
The naive solution is to check every possible rectangle, therefore $\binom N2\binom M2$ checks.
Is there any way or algorithm that I can use to make less checks?

This was an assignment I had last semester, eventually I used the naive solution, but the question still bothers me...

share|cite|improve this question
There is an $O(N^2 M \log M)$-time algorithm. (Hint: First consider the case of a 2×M matrix.) I do not know if there is a faster one. – Tsuyoshi Ito Feb 19 '12 at 1:03
I don't think I follow your thought.. – Keeper Feb 19 '12 at 1:13
Related: $17x17$ Challenge. – draks ... Mar 16 '12 at 11:10
You shouldn't say "square", it suggests a square. You'd be better off writing "rectangle", because that's what it is. – Patrick Da Silva Apr 15 '12 at 16:57
@PatrickDaSilva thanks, changed. – Keeper Apr 15 '12 at 21:01

Here's an $O(N^2 M \log M)$-time algorithm, probably along the lines of what Tsuyoshi was thinking of: For every pair of rows, go through the columns and construct a binary tree of all pairs of identical entries you encounter.

share|cite|improve this answer

Another approach would be to create list of all positions for every unique integer in the matrix ($O(MN)$-time to create all the lists).

You can skip every list of length less than 4 (cannot form a square).

For the remaining integers, you search four positions

$$(a,b)\\(a+x,b)\\(a, b+y)\\(a+x,b+y)$$

looking for any $x,y\in \mathbb{N}$ such that there are the four elements (listed above).

Now you can still do the exhaustive search on the list of positions (first for every $x$ and then looking for $y$), but this sub-problem is much smaller and can be further optimized.

For example, you can skip every element $(a,b)$ in the list which has unique value of $a$ or $b$ as these does not belong to any square (can be done quickly using hashtable even in the first sweep of the algorithm where we are making the lists).

What remains after such filtering are points which have at least one "friend" in the same row or column.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.