
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts in C++
Suppose we have a rectangular cake with height h and width w, we also have two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] represents the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] represents distance from the left of the rectangular cake to the jth vertical cut.
We have to find the maximum area of a piece of cake after we cut it at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. The answer may be large, so return this modulo 10^9 + 7.
So, if the input is like h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
then the output will be 4, as from that image we can understand the given rectangular cake.
Red lines are the horizontal and vertical cuts. After we cut the cake, the green piece of cake has the maximum area.
To solve this, we will follow these steps −
Define a function mul(), this will take a, b,
return ((a mod m) * (b mod m)) mod m
From the main method we will take h, w, an array hh, an array vv,
sort the array hh and vv
insert first element of hh, at index 0 into hh
insert h at the end of hh
insert first element of vv, at index 0 into vv
insert w at the end of vv
a := 0, b := 0
for initialize i := 1, when i < size of hh, update (increase i by 1), do −
a := maximum of a and hh[i] - hh[i - 1]
for initialize i := 1, when i < size of vv, update (increase i by 1), do −
b := maximum of b and vv[i] - vv[i - 1]
return mul(a, b)
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; typedef long long int lli; class Solution { public: lli mul(lli a, lli b){ return ((a % mod) * (b % mod)) % mod; } int maxArea(int h, int w, vector<int>& hh, vector<int>& vv) { sort(hh.begin(), hh.end()); sort(vv.begin(), vv.end()); hh.insert(hh.begin(), 0); hh.push_back(h); vv.insert(vv.begin(), 0); vv.push_back(w); int a = 0; int b = 0; for (int i = 1; i < hh.size(); i++) { a = max(a, hh[i] - hh[i - 1]); } for (int i = 1; i < vv.size(); i++) { b = max(b, vv[i] - vv[i - 1]); } return mul(a, b); } }; main(){ Solution ob; vector<int> v = {1,2,4}, v1 = {1,3}; cout << (ob.maxArea(5,4,v,v1)); }
Input
5,4,{1,2,4}, {1,3}
Output
4
- Related Articles
- Naina was given $1frac{1}{2}$ piece of a cake and Najma was given $1frac{1}{3}$ piece of a cake. Find the total amount of cake given to both of them.
- Naina was given ( 1 frac{1}{2} ) piece of cake and Najma was given ( 1 frac{1}{3} ) piece of cake. Find the total amount of cake was given to both of them.
- Maximum number of pieces in N cuts in C++
- Count pieces of the circle after N cuts in C++
- CSS Central, Horizontal and Vertical Alignment
- Difference between Horizontal and Vertical Relationships
- How to independently set horizontal and vertical, major and minor gridlines of a plot?
- Write the difference between horizontal and vertical analysis of financial statements.
- Vertical and Horizontal Scrollbars on Tkinter Widget
- Difference between vertical integration and horizontal integration
- Difference between Horizontal Equity and Vertical Equity
- Distribute extra horizontal and vertical space in a GridBagLayout with Java
- Horizontal and Vertical Center Alignment with Flexbox in CSS3
- Check horizontal and vertical symmetry in binary matrix in C++
- What letters of the English alphabet have reflectional symmetry $(i.e., symmetry related to mirror reflection)$ about.$(a)$. a vertical mirror$(b)$. a horizontal mirror$(c)$. both horizontal and vertical mirrors
