Amazon Online Assessment 2021 (OA) - Shipment Imbalance
Amazon logistics has multiple delivery centers from which products are sent.
In one such delivery center, parcels are placed in a sequence where the i-th parcel has a weight of weight[i]. A shipment is constituted of a contiguous segment of parcels. The shipment imbalance of a shipment is defined as the difference between the max and min weights within a shipment.
Given the arrangement of parcels, find the sum of shipment imbalance of all the shipments that can be formed from the given sequence of parcels.
Relevant Amazon OA Problems:
Input
weights
: an array of integers that denote the weights of parcels
Output
the sum of shipment imbalance
Examples
Example 1:
Input:
1weights = [1, 3, 2]
Output: 5
Explanation:
The shipment imbalance calculations for each possible shipment are shown below.
Shipments | Max Weight | Min Weight | Imbalance |
---|---|---|---|
1 ,3, 2 | 1 | 1 | 1-1=0 |
1, 3, 2 | 3 | 3 | 3-3=0 |
1, 3, 2 | 2 | 2 | 2-2=0 |
1, 3, 2 | 3 | 1 | 3-1=2 |
1, 3, 2 | 3 | 2 | 3-2=1 |
1, 3, 2 | 3 | 1 | 3-1=2 |
The total imbalance is 0+0+0+2+1+2=5
.
Try it yourself
Solution
Title
Script
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem
Ipsum
has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
Contrary to popular belief, Lorem
Ipsum
is not simply random text.
1 >>> a = [1, 2, 3] 2 >>> a[-1] 3 3