Equal Sum Grid Partition I - Problem
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.
Input & Output
Example 1 — Equal Horizontal Partition
$
Input:
grid = [[2,4],[2,4]]
›
Output:
true
💡 Note:
Horizontal cut after row 0: top section sum = 2+4 = 6, bottom section sum = 2+4 = 6. Both equal, so return true.
Example 2 — No Equal Partition
$
Input:
grid = [[1,2],[3,4]]
›
Output:
false
💡 Note:
Total sum = 10. No horizontal or vertical cut creates equal partitions of 5 each.
Example 3 — Equal Vertical Partition
$
Input:
grid = [[1,3],[2,4]]
›
Output:
true
💡 Note:
Vertical cut after column 0: left section sum = 1+2 = 3, right section sum = 3+4 = 7. Not equal. But horizontal cut after row 0: top = 4, bottom = 6. Still not equal. Actually, let's check: left=[1,2]=3, right=[3,4]=7. No equal partition exists.
Constraints
- 1 ≤ m, n ≤ 100
- 1 ≤ grid[i][j] ≤ 100
Visualization
Tap to expand
Understanding the Visualization
1
Input Grid
2D matrix with positive integers
2
Try Cuts
Test horizontal and vertical cuts
3
Check Equality
Return true if any cut creates equal sums
Key Takeaway
🎯 Key Insight: Check if total sum is even first, then use prefix sums to efficiently test all cuts
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code