✅ [JAVA ][C++] || 100% fast Simple Solution || FULL EXPLAINED  || maxlength | maxbreadth
Upvote if Found Helpful\n\nIdea behind the approch\n\n- If a cake is only cut horizontally and vertically, then the longest length of the pieces will be the lon
**Upvote if Found Helpful**\n\n**Idea behind the approch**\n\n- *If a cake is only cut horizontally and vertically, then the longest length of the pieces will be the longest gap in the horizontal cuts and the longest width of the pieces will be the longest gap in the vertical cuts.*\n- *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.*\n+ *Sort both horizontalCuts and verticalCuts -> to get consecutive cuts*\n*  *find maxLength and MaxWidth\nArea = maxLength * MaxWidth\nmaxLength = maximum gap between consecutive horizontal cut\nmaxWidth = maximum gap between consecutive vertical cut*\n*  *Edge case : remaining Length and width also be checked  from total height and width  after the last cut provide in the array.\n maxLength = Math.max(maxLength, h - horizontalCuts[lastelement]);\n maxWidth = Math.max(maxWidth, w - verticalCuts[lastElement]);*\n\n\n**Code in Java :**\n```\nclass Solution {\n    public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) {\n        \n        final int mod = (int) (1e9 + 7);\n        \n        Arrays.sort(horizontalCuts);\n        Arrays.sort(verticalCuts);\n        \n        int maxLength = horizontalCuts[0], maxWidth = verticalCuts[0] ,\n        n = horizontalCuts.length, m =verticalCuts.length ;\n        \n        for(int i=0; i < n-1; i++){\n            int length = horizontalCuts[i+1] - horizontalCuts[i];\n            maxLength = Math.max(length, maxLength);\n        }\n        \n        //EDGE CASE\n        maxLength = Math.max(maxLength, h - horizontalCuts[n - 1]);\n        \n        for(int i=0; i < m-1; i++){\n            int width = verticalCuts[i+1] - verticalCuts[i];\n            maxWidth = Math.max(width, maxWidth);\n        }\n        \n        //EDGE CASE\n         maxWidth = Math.max(maxWidth, w - verticalCuts[m - 1]);\n        \n        return (int)((1L*maxLength* maxWidth) % mod);  \n    }\n}\n```\n**Code in cpp :**\n```\nclass Solution {\npublic:\n    int maxArea(int h, int w, vector<int>& horizontalCuts, vector<int>& verticalCuts) {\n        \n        const int mod = 1e9 + 7;\n        \n        sort(horizontalCuts.begin(), horizontalCuts.end());\n        sort(verticalCuts.begin() , verticalCuts.end());\n        \n        int maxLength = horizontalCuts[0], maxWidth = verticalCuts[0] ,\n        n = horizontalCuts.size(), m =verticalCuts.size() ;\n        \n        for(int i=0; i < n-1; i++){\n            int length = horizontalCuts[i+1] - horizontalCuts[i];\n            maxLength = max(length, maxLength);\n        }\n        \n        //EDGE CASE\n        maxLength = max(maxLength, h - horizontalCuts[n - 1]);\n        \n        for(int i=0; i < m-1; i++){\n            int width = verticalCuts[i+1] - verticalCuts[i];\n            maxWidth = max(width, maxWidth);\n        }\n        \n        //EDGE CASE\n         maxWidth = max(maxWidth, w - verticalCuts[m - 1]);\n        \n        return (int)((1L*maxLength* maxWidth) % mod);\n    }\n};\n```\n**time Complexity : O(nlogn) because of sorting.**\n**Space Complexity : O(1).**\n
[]