Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts - Problem

Imagine you're a pastry chef with a rectangular cake of dimensions h ร— w. You need to make precise cuts to create individual pieces, and you want to find the largest possible piece after all cuts are made.

You have two arrays:

  • horizontalCuts[i] - distance from the top of the cake to the i-th horizontal cut
  • verticalCuts[j] - distance from the left of the cake to the j-th vertical cut

Goal: Return the maximum area of any single piece after making all the specified cuts. Since the result can be very large, return it modulo 10^9 + 7.

Key insight: The largest piece will be formed by the largest gap between consecutive horizontal cuts multiplied by the largest gap between consecutive vertical cuts!

Input & Output

example_1.py โ€” Basic Case
$ Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
โ€บ Output: 4
๐Ÿ’ก Note: After sorting and adding boundaries: horizontal cuts at [0,1,2,4,5] and vertical cuts at [0,1,3,4]. The maximum horizontal gap is 2 (between positions 2 and 4), and maximum vertical gap is 2 (between positions 1 and 3). Maximum area = 2 ร— 2 = 4.
example_2.py โ€” Larger Cake
$ Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
โ€บ Output: 6
๐Ÿ’ก Note: After sorting: horizontal cuts at [0,1,3,5] and vertical cuts at [0,1,4]. Maximum horizontal gap is 2 (between 3 and 5), maximum vertical gap is 3 (between 1 and 4). Maximum area = 2 ร— 3 = 6.
example_3.py โ€” Edge Case
$ Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
โ€บ Output: 9
๐Ÿ’ก Note: Horizontal cuts at [0,3,5] give max gap of 3. Vertical cuts at [0,3,4] give max gap of 3. Maximum area = 3 ร— 3 = 9.

Constraints

  • 2 โ‰ค h, w โ‰ค 109
  • 0 โ‰ค horizontalCuts.length โ‰ค 105
  • 0 โ‰ค verticalCuts.length โ‰ค 105
  • 1 โ‰ค horizontalCuts[i] < h
  • 1 โ‰ค verticalCuts[i] < w
  • All cuts are at distinct positions
  • Return result modulo 109 + 7

Visualization

Tap to expand
๐ŸŽ‚ Maximum Cake Piece Strategy๐Ÿฐ LARGEST PIECEArea = Max H-Gap ร— Max V-GapMax H-Gap = 120Max V-Gap = 300๐Ÿ’ก Key: Don't check all pieces - just find the biggest gaps!
Understanding the Visualization
1
Add Boundary Cuts
Imagine cuts at all four edges of the cake (positions 0, h, 0, w)
2
Sort All Cuts
Arrange all cuts in order from left to right and top to bottom
3
Find Largest Gaps
Look for the biggest space between consecutive cuts in each direction
4
Calculate Max Area
The intersection of the largest horizontal and vertical gaps gives the maximum area
Key Takeaway
๐ŸŽฏ Key Insight: The maximum area is always at the intersection of the largest horizontal gap and largest vertical gap - no need to check every possible piece!
Asked in
Google 45 Amazon 38 Meta 29 Microsoft 22
38.4K Views
Medium-High Frequency
~15 min Avg. Time
1.8K 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