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

| You are given a board of size M Γ— N where each cell can be either empty ('O') o… |
| Undirected Coloured Graph Shortest Path You are given an undirected weight… |
| Village Voyage A computer game "Village Voyage" has N villages (labeled 1 to… |
| Academic Decathlon Students are being selected for an academic decathlon tea… |
| Sum of Arrays Given two arrays each of length n, arr1 and arr2, in one opera… |
| Count Swaps During Custom Sorting Analyze the efficiency of the following so… |