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
Equal Sum Grid Partition ProblemInput: 2×2 Grid2424Try Horizontal Cut2424Top: 2+4 = 6Bottom: 2+4 = 6Try Vertical Cut2424Left: 2+2 = 4Right: 4+4 = 8✓ Horizontal cut creates equal partitions (6 = 6)✗ Vertical cut creates unequal partitions (4 ≠ 8)Result: trueAt least one cut creates equal-sum sections
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
Asked in
Google 25 Amazon 18 Microsoft 15 Apple 12
23.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen