1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts LeetCode Solution
In this guide, you will get 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts LeetCode Solution with the best time and space complexity. The solution to Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts 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
- Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts solution in C++
- Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts solution in Java
- Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts solution in Python
- Additional Resources

Problem Statement of Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
You are given a rectangular cake of size h x w and two arrays of integers horizontalCuts and verticalCuts where:
horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, and
verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a large number, return this modulo 109 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 109
1 <= horizontalCuts.length <= min(h – 1, 105)
1 <= verticalCuts.length <= min(w – 1, 105)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
All the elements in horizontalCuts are distinct.
All the elements in verticalCuts are distinct.
Complexity Analysis
- Time Complexity: O(\texttt{sort}(\texttt{horizontalCuts}) + \texttt{sort}(\texttt{verticalCuts}))
- Space Complexity: O(\texttt{sort}(\texttt{horizontalCuts}) + \texttt{sort}(\texttt{verticalCuts}))
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts LeetCode Solution in C++
class Solution {
public:
int maxArea(int h, int w, vector<int>& horizontalCuts,
vector<int>& verticalCuts) {
constexpr int kMod = 1'000'000'007;
ranges::sort(horizontalCuts);
ranges::sort(verticalCuts);
// the maximum gap of each direction
int maxGapX = max(verticalCuts[0], w - verticalCuts.back());
int maxGapY = max(horizontalCuts[0], h - horizontalCuts.back());
for (int i = 1; i < verticalCuts.size(); ++i)
maxGapX = max(maxGapX, verticalCuts[i] - verticalCuts[i - 1]);
for (int i = 1; i < horizontalCuts.size(); ++i)
maxGapY = max(maxGapY, horizontalCuts[i] - horizontalCuts[i - 1]);
return static_cast<long>(maxGapX) * maxGapY % kMod;
}
};
/* code provided by PROGIEZ */
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts LeetCode Solution in Java
class Solution {
public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) {
final int kMod = 1_000_000_007;
Arrays.sort(horizontalCuts);
Arrays.sort(verticalCuts);
// the maximum gap of each direction
int maxGapX = Math.max(verticalCuts[0], w - verticalCuts[verticalCuts.length - 1]);
int maxGapY = Math.max(horizontalCuts[0], h - horizontalCuts[horizontalCuts.length - 1]);
for (int i = 1; i < verticalCuts.length; ++i)
maxGapX = Math.max(maxGapX, verticalCuts[i] - verticalCuts[i - 1]);
for (int i = 1; i < horizontalCuts.length; ++i)
maxGapY = Math.max(maxGapY, horizontalCuts[i] - horizontalCuts[i - 1]);
return (int) ((long) maxGapX * maxGapY % kMod);
}
}
// code provided by PROGIEZ
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts LeetCode Solution in Python
class Solution:
def maxArea(
self,
h: int,
w: int,
horizontalCuts: list[int],
verticalCuts: list[int],
) -> int:
kMod = 1_000_000_007
# the maximum gap of each direction
maxGapX = max(b - a
for a, b in itertools.pairwise(
[0] + sorted(horizontalCuts) + [h]))
maxGapY = max(b - a
for a, b in itertools.pairwise(
[0] + sorted(verticalCuts) + [w]))
return maxGapX * maxGapY % kMod
# 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.