1727. Largest Submatrix With Rearrangements LeetCode Solution
In this guide, you will get 1727. Largest Submatrix With Rearrangements LeetCode Solution with the best time and space complexity. The solution to Largest Submatrix With Rearrangements problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Largest Submatrix With Rearrangements solution in C++
- Largest Submatrix With Rearrangements solution in Java
- Largest Submatrix With Rearrangements solution in Python
- Additional Resources

Problem Statement of Largest Submatrix With Rearrangements
You are given a binary matrix matrix of size m x n, and you are allowed to rearrange the columns of the matrix in any order.
Return the area of the largest submatrix within matrix where every element of the submatrix is 1 after reordering the columns optimally.
Example 1:
Input: matrix = [[0,0,1],[1,1,1],[1,0,1]]
Output: 4
Explanation: You can rearrange the columns as shown above.
The largest submatrix of 1s, in bold, has an area of 4.
Example 2:
Input: matrix = [[1,0,1,0,1]]
Output: 3
Explanation: You can rearrange the columns as shown above.
The largest submatrix of 1s, in bold, has an area of 3.
Example 3:
Input: matrix = [[1,1,0],[1,0,1]]
Output: 2
Explanation: Notice that you must rearrange entire columns, and there is no way to make a submatrix of 1s larger than an area of 2.
Constraints:
m == matrix.length
n == matrix[i].length
1 <= m * n <= 105
matrix[i][j] is either 0 or 1.
Complexity Analysis
- Time Complexity: O(mn\log n)
- Space Complexity: O(n)
1727. Largest Submatrix With Rearrangements LeetCode Solution in C++
class Solution {
public:
int largestSubmatrix(vector<vector<int>>& matrix) {
const int n = matrix[0].size();
int ans = 0;
vector<int> hist(n);
for (const vector<int>& row : matrix) {
// Accumulate the histogram if possible.
for (int i = 0; i < n; ++i)
hist[i] = row[i] == 0 ? 0 : hist[i] + 1;
// Get the sorted histogram.
vector<int> sortedHist(hist);
ranges::sort(sortedHist);
// Greedily calculate the answer.
for (int i = 0; i < n; ++i)
ans = max(ans, sortedHist[i] * (n - i));
}
return ans;
}
};
/* code provided by PROGIEZ */
1727. Largest Submatrix With Rearrangements LeetCode Solution in Java
class Solution {
public int largestSubmatrix(int[][] matrix) {
final int n = matrix[0].length;
int ans = 0;
int[] hist = new int[n];
for (int[] row : matrix) {
// Accumulate the histogram if possible.
for (int i = 0; i < n; ++i)
hist[i] = row[i] == 0 ? 0 : hist[i] + 1;
// Get the sorted histogram.
int[] sortedHist = hist.clone();
Arrays.sort(sortedHist);
// Greedily calculate the answer.
for (int i = 0; i < n; ++i)
ans = Math.max(ans, sortedHist[i] * (n - i));
}
return ans;
}
}
// code provided by PROGIEZ
1727. Largest Submatrix With Rearrangements LeetCode Solution in Python
class Solution:
def largestSubmatrix(self, matrix: list[list[int]]) -> int:
ans = 0
hist = [0] * len(matrix[0])
for row in matrix:
# Accumulate the histogram if possible.
for i, num in enumerate(row):
hist[i] = 0 if num == 0 else hist[i] + 1
# Get the sorted histogram.
sortedHist = sorted(hist)
# Greedily calculate the answer.
for i, h in enumerate(sortedHist):
ans = max(ans, h * (len(row) - i))
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.