Contents
Storage Optimization Amazon OA 2023 Solution
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.
SOLUTION
Program: Storage Optimization Amazon OA Solution in C++
class Solution {
public:
int maxArea(int h, int w, vector<int>& hc, vector<int>& vc) {
int mod = pow(10, 9) + 7;
sort(hc.begin(), hc.end());
sort(vc.begin(), vc.end());
int maxl = INT_MIN, maxw = INT_MIN;
long long res;
for(int i = 0; i < hc.size(); i++) {
if(i == 0) maxl = max(maxl, hc[i] - 0);
if(i == hc.size() - 1) maxl = max(maxl,h - hc[i]);
if(i > 0) maxl = max(maxl, hc[i] - hc[i - 1]);
}
for(int i = 0; i < vc.size(); i++) {
if(i == 0) maxw = max(maxw, vc[i] - 0);
if(i == vc.size() - 1) maxw = max(maxw, w - vc[i]);
if(i > 0) maxw = max(maxw, vc[i] - vc[i - 1]);
}
res = (long(maxl)* long(maxw)) % mod;
return res;
}
};
Program: Storage Optimization Amazon OA Solution in Java
class Solution {
public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) {
Arrays.sort(horizontalCuts);
Arrays.sort(verticalCuts);
long maxHorizontalGap = horizontalCuts[0];
int index = 0;
for(index = 1; index < horizontalCuts.length; index++){
maxHorizontalGap = (long)Math.max(maxHorizontalGap, horizontalCuts[index] - horizontalCuts[index - 1]);
}
maxHorizontalGap = (long)Math.max(maxHorizontalGap, h - horizontalCuts[index - 1]);
long maxVerticalGap = verticalCuts[0];
index = 0;
for(index = 1; index < verticalCuts.length; index++){
maxVerticalGap = (long)Math.max(maxVerticalGap, verticalCuts[index] - verticalCuts[index - 1]);
}
maxVerticalGap = (long)Math.max(maxVerticalGap, w - verticalCuts[index - 1]);
return (int)((maxHorizontalGap * maxVerticalGap) % 1000000007 );
}
}
Program: Storage Optimization Amazon OA Solution in Python
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
#SORT BOTH INPUT LISTS
horizontalCuts.sort()
verticalCuts.sort()
#INSERT '0' AT BEGINNING AND 'h' AT END OF 'horizontalCuts' LIST
horizontalCuts.insert(0,0)
horizontalCuts.append(h)
#CREATE A NEW LIST OF DIFFERENCES BETWEEN ALL 'horizontalCuts' ELEMENTS
horDifs = []
for x in range(1,len(horizontalCuts)):
horDifs.append(horizontalCuts[x] - horizontalCuts[x-1])
#DO THE SAME WITH 'verticalCuts' LIST
verticalCuts.insert(0,0)
verticalCuts.append(w)
verDifs = []
for x in range(1,len(verticalCuts)):
verDifs.append(verticalCuts[x] - verticalCuts[x-1])
#TAKE THE MAX VALUES FROM BOTH LISTS OF DIFFERENCES AND MULTIPLY
#TAKE MODULO AT THE END
return max(horDifs) * max(verDifs) % (10**9 + 7)
Amazon OA 2023 Questions with Solution
- Shopping Patterns Solution Amazon OA 2023
- Reorder Data in Log Files Solution Amazon OA 2023
- Top K Frequent Words Solution Amazon OA 2023
- Trees Height Solution Amazon OA SDE 2023
- Counting Binary Substrings Amazon OA 2023
- Grid Connections Amazon OA 2023
- Shipment Imbalance Amazon OA 2023
- Max Profit Amazon OA 2023
- Find Lowest Price Amazon OA 2023
- Decode String Frequency Amazon OA 2023
- Simple Cipher Amazon OA 2023
- Valid Discount Coupons Amazon OA 2023 Solution
- Count Maximum Teams Amazon OA 2023
- Minimum Coin Flips Amazon OA 2023
- Max Average Stock Price Amazon OA 2023 Solution
- Robot Bounded In Circle Amazon OA 2023
- Shopping Options Amazon OA 2023 Solution
- Fill The Truck Maximum Units on a Truck Amazon OA Solution
- Maximize Score After N Operations Number Game Solution Amazon OA 2023
- Slowest Key Amazon OA 2023 Solution
- Five Star Seller Maximum Average Pass Ratio Amazon OA 2023
- Split String Into Unique Primes Amazon OA 2023 Solution
- Minimum Difficulty of a Job Schedule Amazon OA 2023 Solution
- Autoscale Policy Utilization Check Amazon OA 2023
- Optimal Utilization Solution Amazon OA 2023
- Merge Two Sorted Lists Solution Amazon OA 2023
- Two Sum Unique Pairs Solution Amazon OA 2023
- Amazon Music Pairs Amazon OA 2023 Solution
- Class Grouping Amazon OA 2023 Solution
- Find Max products Amazon OA 2023 Solution
- Get encrypted number Amazon OA 2023 Solution
- Find Total Imbalance Amazon OA 2023 Solution
- Find Total Power Amazon OA 2023 Solution
I was looking for this question for so long and I found this site which already had solutions and a list of amazon oa 2021 questions, thank you so much it’s so valuable to me.