AMAZON Coding Question – Solved
Amazon has multiple delivery centers for the distribution of its goods. In one such center, parcels are arranged in a sequence where the ith parcel has a weight of weight[i]. A shipment is constituted of a contiguous segment of parcels in this arrangement.
For example, with weights [3, 6, 3], possible shipments include [3], [6], [3], [3, 6], [6, 3], and [3, 6, 3], but not [3, 3] (since it’s not contiguous).
These shipments are to be loaded for delivery and must be **balanced**.
A shipment is considered balanced **if the weight of the last parcel in the shipment is *not* the maximum weight** in that shipment.
- Example: [3, 9, 4, 7] is balanced because the last parcel is 7 and the max is 9.
- [4, 7, 2, 7] is **not** balanced since the last parcel (7) is also the maximum.
**Task:**
Given an array `weight` of size `n`, representing parcel weights, determine the **maximum number of balanced shipments** that can be formed such that:
- Each parcel belongs to exactly one shipment
- Each shipment is a contiguous subarray
- Each shipment is balanced
If no valid balanced shipment can be formed, return 0.
**Example:**
`weight = [1, 2, 3, 2, 6, 3]`
There are n = 6 parcels. The optimal way is to divide them into two shipments:
[1, 2, 3, 2] and [6, 3], both of which are balanced.
Hence, the output is `2`.
**Function Description:**
Complete the function `getMaximumBalancedShipments` in the editor below.
**Function Signature:**
`int getMaximumBalancedShipments(int[] weight)`
Asked in:
AMAZON