# Storage Optimization Amazon OA 2023 Solution

## 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

### 1 thought on “Storage Optimization Amazon OA 2023 Solution”

1. 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.