Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an m x n matrix grid of positive integers. Your task is to determine if it is possible to make either one horizontal or one vertical cut on the grid such that:
Return true if such a partition exists; otherwise return false.
Example 1:
Input: grid = [[1,4],[2,3]]
Output: true
Explanation:
A horizontal cut between row 0 and row 1 results in two non-empty sections, each with a sum of 5. Thus, the answer is true.
Example 2:
Input: grid = [[1,3],[2,4]]
Output: false
Explanation:
No horizontal or vertical cut results in two non-empty sections with equal sums. Thus, the answer is false.
Constraints:
1 <= m == grid.length <= 1051 <= n == grid[i].length <= 1052 <= m * n <= 1051 <= grid[i][j] <= 105Problem summary: You are given an m x n matrix grid of positive integers. Your task is to determine if it is possible to make either one horizontal or one vertical cut on the grid such that: Each of the two resulting sections formed by the cut is non-empty. The sum of the elements in both sections is equal. Return true if such a partition exists; otherwise return false.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[[1,4],[2,3]]
[[1,3],[2,4]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3546: Equal Sum Grid Partition I
class Solution {
public boolean canPartitionGrid(int[][] grid) {
long s = 0;
for (var row : grid) {
for (int x : row) {
s += x;
}
}
if (s % 2 != 0) {
return false;
}
int m = grid.length, n = grid[0].length;
long pre = 0;
for (int i = 0; i < m; ++i) {
for (int x : grid[i]) {
pre += x;
}
if (pre * 2 == s && i < m - 1) {
return true;
}
}
pre = 0;
for (int j = 0; j < n; ++j) {
for (int i = 0; i < m; ++i) {
pre += grid[i][j];
}
if (pre * 2 == s && j < n - 1) {
return true;
}
}
return false;
}
}
// Accepted solution for LeetCode #3546: Equal Sum Grid Partition I
func canPartitionGrid(grid [][]int) bool {
s := 0
for _, row := range grid {
for _, x := range row {
s += x
}
}
if s%2 != 0 {
return false
}
m, n := len(grid), len(grid[0])
pre := 0
for i, row := range grid {
for _, x := range row {
pre += x
}
if pre*2 == s && i+1 < m {
return true
}
}
pre = 0
for j := 0; j < n; j++ {
for i := 0; i < m; i++ {
pre += grid[i][j]
}
if pre*2 == s && j+1 < n {
return true
}
}
return false
}
# Accepted solution for LeetCode #3546: Equal Sum Grid Partition I
class Solution:
def canPartitionGrid(self, grid: List[List[int]]) -> bool:
s = sum(sum(row) for row in grid)
if s % 2:
return False
pre = 0
for i, row in enumerate(grid):
pre += sum(row)
if pre * 2 == s and i != len(grid) - 1:
return True
pre = 0
for j, col in enumerate(zip(*grid)):
pre += sum(col)
if pre * 2 == s and j != len(grid[0]) - 1:
return True
return False
// Accepted solution for LeetCode #3546: Equal Sum Grid Partition I
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3546: Equal Sum Grid Partition I
// class Solution {
// public boolean canPartitionGrid(int[][] grid) {
// long s = 0;
// for (var row : grid) {
// for (int x : row) {
// s += x;
// }
// }
// if (s % 2 != 0) {
// return false;
// }
// int m = grid.length, n = grid[0].length;
// long pre = 0;
// for (int i = 0; i < m; ++i) {
// for (int x : grid[i]) {
// pre += x;
// }
// if (pre * 2 == s && i < m - 1) {
// return true;
// }
// }
// pre = 0;
// for (int j = 0; j < n; ++j) {
// for (int i = 0; i < m; ++i) {
// pre += grid[i][j];
// }
// if (pre * 2 == s && j < n - 1) {
// return true;
// }
// }
// return false;
// }
// }
// Accepted solution for LeetCode #3546: Equal Sum Grid Partition I
function canPartitionGrid(grid: number[][]): boolean {
let s = 0;
for (const row of grid) {
s += row.reduce((a, b) => a + b, 0);
}
if (s % 2 !== 0) {
return false;
}
const [m, n] = [grid.length, grid[0].length];
let pre = 0;
for (let i = 0; i < m; ++i) {
pre += grid[i].reduce((a, b) => a + b, 0);
if (pre * 2 === s && i + 1 < m) {
return true;
}
}
pre = 0;
for (let j = 0; j < n; ++j) {
for (let i = 0; i < m; ++i) {
pre += grid[i][j];
}
if (pre * 2 === s && j + 1 < n) {
return true;
}
}
return false;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.