- area-volume-programs, Arrays, Geometric, Mathematical, square-rectangle

Maximum area of a Cake after Horizontal and Vertical cuts

Given two positive integers h and w representing the height h and width w which forms a rectangle. Also, there are two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangle to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangle to the jth 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. Since the answer can be a huge number, return this modulo 10^9 + 7.Examples :  max area = 6Input: h = 6, w = 4, horizontalCuts = [2, 5], verticalCuts = [1, 3]Output: 6Explanation: The figure above 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: 9Approach: The problem can be solved by observing that-The horizontalCuts if perpendicular to any VerticalCut, then all the vertical slices cross all the horizontalCuts.Next, the maximum area of the rectangle must be enclosed by at least one vertical and one horizontal cut.From the above observation, it is clear that we need to find the maximum distance between two horizontal cuts and two vertical cuts respectively, and multiply them to find the area of the rectangle. Follow the steps below to solve the problem:Sort both horizontalCuts and verticalCuts array.Initialize two variables, say MaxHorizontal and MaxVertical as 0 which will store the maximum horizontal and vertical lengths of the rectangle respectively.Iterate in the range [1, horizontalCuts.size()-1] using the variable i and perform the following steps:Modify the value of MaxHorizontal as max(MaxHorizontal, horizontalCuts[i] – horizontalCuts[i-1]).Modify the value of MaxVertical as max(MaxVertical, verticalCuts[i] – verticalCuts[i-1]).Print MaxHorizontal*MaxVertical as the answer.Below is the implementation of the above approach:C++#include using namespace std;const int mod = 1e9 + 7;  class Solution {public:            int maxArea(int h, int w, vector& horizontalCuts,                vector& verticalCuts)    {                  sort(horizontalCuts.begin(), horizontalCuts.end());        sort(verticalCuts.begin(), verticalCuts.end());                          horizontalCuts.push_back(h);        verticalCuts.push_back(w);          int maxHorizontal = 0;        int maxVertical = 0;                  for (int i = 1; i < horizontalCuts.size(); i++) {            int diff                = horizontalCuts[i] - horizontalCuts[i - 1];            maxHorizontal = max(maxHorizontal, diff);        }                  for (int i = 1; i < verticalCuts.size(); i++) {            int diff                = verticalCuts[i] - verticalCuts[i - 1];            maxVertical = max(maxVertical, diff);        }                  return (int)((long)maxHorizontal * maxVertical                     % mod);    }};  int main(){        Solution ob;        vector hc = { 2, 5 }, vc = { 1, 3 };    int h = 6, v = 4;        cout