# Sort binary matrix by swapping columns to make subrectangle of ones with maximum size

We have given binary matrix (matrix containing only 1 and 0) of size $n\cdot m$. We want to order the matrix such that the biggest rectangle containing only ones is with maximum size.

For example if the following matrix is given:

1000  0010
1101  0111
1101  0111
1101  0111


We should output 9 because the biggest rectangle we can get is with size 9. We are allowed to swap any columns infinite number of times and we are only requested to find the maximum size of the subrectangle.

Can we solve this problem faster that trying all possible permutations?

• What's the best algorithm you have been able to find so far? What approaches have you tried? What's the context where you encountered this problem? Please credit the source of this problem in the question. – D.W. Apr 17 '18 at 22:30
• The problem is not given on english, also I still haven't found working algorithm rather than not proven greedys – someone12321 Apr 18 '18 at 7:30

The biggest such rectangle must span consecutive rows. So, for all $i,j$ with $i \le j$, check what is the biggest rectangle you can make that spans rows $i..j$. You can do this check in $O(mn)$ time, by checking which columns have a 1 in all of those rows. There are only $O(m^2)$ possible values of $i,j$.
So, the running time of this simple method is $O(m^3 n)$, which is faster than trying all permutations. (Trying all permutations would take $\Omega(m \cdot n!)$ time, which is far slower -- it is exponential time, rather than polynomial time.) I make no claim that this simple method is optimal -- rather, I merely claim that the answer to your question is "yes".