AMAZON Coding Question – Solved

9 Live
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

Image of the Question

Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


Please login to view the solution


Related Questions

| A user is using the Amazon fitness tracker and is engaged in a jumping exercise… |
| Amazon Kindle has several e-books that customers can purchase directly. There a… |
| The engineering team at an Amazon fulfillment center is optimizing n high-perfo… |
| The supply chain manager at one of Amazon's warehouses is shipping the last con… |
| Determine the highest value after executing n steps on an infinite 2D grid that… |
| Amazon Prime Video is developing a new feature called "Segmentify." This featur… |