AMAZON Coding Question – Solved

9 Live
Amazon is working on optimizing its delivery zones to ensure efficient and timely delivery of packages. Each delivery zone is represented as an interval indicating the range of house numbers it covers. The delivery zones can overlap. Given is a list of n delivery zones, where the i-th zone covers the interval (a[i], b[i]) (inclusive). Additionally, given is a maximum allowed length, k, for any new delivery zone that can be added. Your task is to add exactly one new delivery zone (a, b) such that the length of this new zone (b - a) is less than or equal to k. The goal is to minimize the number of disconnected delivery zones after adding the new delivery zone. A set of delivery zones [ (a[1], b[1]), (a[2], b[2]), ..., (a[n], b[n])] is considered connected if every house number in the range (min(a[1], a[2], ..., a[n]), max(b[1], b[2], ..., b[n])) is covered by at least one of the delivery zones (a[i], b[i]) in the set. For an instance: - The set [(1, 2), (2, 3), (1, 5)] is connected because every house number in the interval (min(1, 2, 1), max(2, 3, 5)) = (1, 5) is covered by at least one of the delivery zones. - The set [(2, 2), (3, 4)] is not connected because the delivery zones (2, 2) and (3, 4) do not overlap each other and hence is disconnected. Note: The arrays 'a' and 'b' used above are considered to follow 1-based indexing. Example Consider the delivery zones: [(1, 5), (2, 4), (6, 6), (7, 14), (16, 19)] and k = 2. If you add a new delivery zone (5, 7) to the list, you can separate the zones into 2 connected sets: - [(1, 5), (2, 4), (5, 7), (6, 6), (7, 14)] - [(16, 19)] However, if you add a new delivery zone (14, 16), you will end up with 3 connected sets: - [(1, 5), (2, 4)] - [(6, 6)] - [(14, 16), (16, 19)]

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