Amazon Online Assessment Question I could not solve

You have n servers (every index) with given capacity and on-coming load.

(0-indexed)
capacity = [3, 2, 1, 4]
load = [2, 1, 4, 3]

if load on server i is greater than it's capacity, It must redirect some load to another server j which has capacity to spare.

The latency of to redirect load from i to j is abs(i-j).

what is the minimum maximum latency required to distrivute load such that all server have load less than or equal to their capacity.

Example:
capacity = [3, 2, 1, 4]
load = [2, 2, 4, 2]

ans:
server i=2 redirects 2 requests to server 3
server i=2 redirects 1 request to server 0.

answer = max(|2-3|, |2-0|) => 2

I tried doing binary saerch on answer but missed something. Does anyonw knoow how to solve this?

Comments (5)