Maximum area of a Cake after Horizontal and Vertical cuts
Last Updated :
24 Feb, 2025
Given two positive integers h and w representing the height and width of a rectangle. Additionally, we have two arrays, horizontalCuts and verticalCuts, where:
- horizontalCuts[i] is the distance from the top of the rectangular cake to the i-th horizontal cut.
- verticalCuts[i] is the distance from the left of the rectangular cake to the j-th vertical cut.
The task is to find the maximum area of the rectangle after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.
Examples:
Input: h = 6, w = 4, horizontalCuts = [2, 5], verticalCuts = [1, 3]
Output: 6
Explanation: The figure below represents the given rectangle.

Red lines are the horizontal cuts and blue lines are vertical cuts. After the rectangle is cut, the green piece of rectangle has the maximum area.
Input: h = 5, w = 4, horizontalCuts = [3, 1], verticalCuts = [1]
Output: 9
[Expected Approach] Using Sorting - O(n * log(n) + m * log(m)) Time and O(1) Space
The idea is to maximize the area of a rectangular piece formed by the cuts. Since the largest area is determined by the widest horizontal and vertical gaps, we first sort the cut positions and find the maximum gaps between consecutive cuts. The final area is obtained by multiplying the largest horizontal gap with the largest vertical gap. Sorting ensures efficient gap calculation, making the approach optimal.
Steps to implement the above idea:
- Sort horizontalCuts and verticalCuts in ascending order to process them efficiently.
- Append h and w to their respective lists to ensure the last segment is considered.
- Initialize maxHorizontal and maxVertical with the first cut size to handle the first segment.
- Iterate through horizontalCuts and verticalCuts to find the maximum gap between consecutive cuts.
- Multiply the largest horizontal gap with the largest vertical gap to get the maximum area.
- Return the computed maximum area, which represents the largest rectangular piece after the cuts.
C++
// C++ Code to find the maximum area of rectangle
// after Horizontal and Vertical Cuts
#include <bits/stdc++.h>
using namespace std;
int maxArea(int h, int w, vector<int>& horizontalCuts,
vector<int>& verticalCuts) {
sort(horizontalCuts.begin(), horizontalCuts.end());
sort(verticalCuts.begin(), verticalCuts.end());
// Insert the right bound h and w
// in their respective vectors
horizontalCuts.push_back(h);
verticalCuts.push_back(w);
int maxHorizontal = horizontalCuts[0];
int maxVertical = verticalCuts[0];
// Find the maximum Horizontal Length possible
for (int i = 1; i < horizontalCuts.size(); i++) {
int diff = horizontalCuts[i] - horizontalCuts[i - 1];
maxHorizontal = max(maxHorizontal, diff);
}
// Find the maximum Vertical Length possible
for (int i = 1; i < verticalCuts.size(); i++) {
int diff = verticalCuts[i] - verticalCuts[i - 1];
maxVertical = max(maxVertical, diff);
}
// Return the maximum area of rectangle
return maxHorizontal * maxVertical;
}
// Driver Code
int main() {
vector<int> horizontalCuts = {2, 5};
vector<int> verticalCuts = {1, 3};
int h = 6, w = 4;
// Function Call
cout << maxArea(h, w, horizontalCuts, verticalCuts);
return 0;
}
Java
// Java Code to find the maximum area of rectangle
// after Horizontal and Vertical Cuts
import java.util.*;
class GfG {
static int maxArea(int h, int w, List<Integer> horizontalCuts,
List<Integer> verticalCuts) {
Collections.sort(horizontalCuts);
Collections.sort(verticalCuts);
// Insert the right bound h and w
// in their respective lists
horizontalCuts.add(h);
verticalCuts.add(w);
int maxHorizontal = horizontalCuts.get(0);
int maxVertical = verticalCuts.get(0);
// Find the maximum Horizontal Length possible
for (int i = 1; i < horizontalCuts.size(); i++) {
int diff = horizontalCuts.get(i) - horizontalCuts.get(i - 1);
maxHorizontal = Math.max(maxHorizontal, diff);
}
// Find the maximum Vertical Length possible
for (int i = 1; i < verticalCuts.size(); i++) {
int diff = verticalCuts.get(i) - verticalCuts.get(i - 1);
maxVertical = Math.max(maxVertical, diff);
}
// Return the maximum area of rectangle
return maxHorizontal * maxVertical;
}
// Driver Code
public static void main(String[] args) {
List<Integer> horizontalCuts = new ArrayList<>(Arrays.asList(2, 5));
List<Integer> verticalCuts = new ArrayList<>(Arrays.asList(1, 3));
int h = 6, w = 4;
// Function Call
System.out.println(maxArea(h, w, horizontalCuts, verticalCuts));
}
}
Python
# Python Code to find the maximum area of rectangle
# after Horizontal and Vertical Cuts
def maxArea(h, w, horizontalCuts, verticalCuts):
horizontalCuts.sort()
verticalCuts.sort()
# Insert the right bound h and w
# in their respective lists
horizontalCuts.append(h)
verticalCuts.append(w)
maxHorizontal = horizontalCuts[0]
maxVertical = verticalCuts[0]
# Find the maximum Horizontal Length possible
for i in range(1, len(horizontalCuts)):
diff = horizontalCuts[i] - horizontalCuts[i - 1]
maxHorizontal = max(maxHorizontal, diff)
# Find the maximum Vertical Length possible
for i in range(1, len(verticalCuts)):
diff = verticalCuts[i] - verticalCuts[i - 1]
maxVertical = max(maxVertical, diff)
# Return the maximum area of rectangle
return maxHorizontal * maxVertical
# Driver Code
if __name__ == "__main__":
horizontalCuts = [2, 5]
verticalCuts = [1, 3]
h, w = 6, 4
# Function Call
print(maxArea(h, w, horizontalCuts, verticalCuts))
C#
// C# Code to find the maximum area of rectangle
// after Horizontal and Vertical Cuts
using System;
using System.Collections.Generic;
class GfG {
static int MaxArea(int h, int w, List<int> horizontalCuts,
List<int> verticalCuts) {
horizontalCuts.Sort();
verticalCuts.Sort();
// Insert the right bound h and w
// in their respective lists
horizontalCuts.Add(h);
verticalCuts.Add(w);
int maxHorizontal = horizontalCuts[0];
int maxVertical = verticalCuts[0];
// Find the maximum Horizontal Length possible
for (int i = 1; i < horizontalCuts.Count; i++) {
int diff = horizontalCuts[i] - horizontalCuts[i - 1];
maxHorizontal = Math.Max(maxHorizontal, diff);
}
// Find the maximum Vertical Length possible
for (int i = 1; i < verticalCuts.Count; i++) {
int diff = verticalCuts[i] - verticalCuts[i - 1];
maxVertical = Math.Max(maxVertical, diff);
}
// Return the maximum area of rectangle
return maxHorizontal * maxVertical;
}
// Driver Code
public static void Main() {
List<int> horizontalCuts = new List<int> { 2, 5 };
List<int> verticalCuts = new List<int> { 1, 3 };
int h = 6, w = 4;
// Function Call
Console.WriteLine(MaxArea(h, w, horizontalCuts, verticalCuts));
}
}
JavaScript
// JavaScript Code to find the maximum area of rectangle
// after Horizontal and Vertical Cuts
function maxArea(h, w, horizontalCuts, verticalCuts) {
horizontalCuts.sort((a, b) => a - b);
verticalCuts.sort((a, b) => a - b);
// Insert the right bound h and w
// in their respective arrays
horizontalCuts.push(h);
verticalCuts.push(w);
let maxHorizontal = horizontalCuts[0];
let maxVertical = verticalCuts[0];
// Find the maximum Horizontal Length possible
for (let i = 1; i < horizontalCuts.length; i++) {
let diff = horizontalCuts[i] - horizontalCuts[i - 1];
maxHorizontal = Math.max(maxHorizontal, diff);
}
// Find the maximum Vertical Length possible
for (let i = 1; i < verticalCuts.length; i++) {
let diff = verticalCuts[i] - verticalCuts[i - 1];
maxVertical = Math.max(maxVertical, diff);
}
// Return the maximum area of rectangle
return maxHorizontal * maxVertical;
}
// Driver Code
let horizontalCuts = [2, 5];
let verticalCuts = [1, 3];
let h = 6, w = 4;
// Function Call
console.log(maxArea(h, w, horizontalCuts, verticalCuts));
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem