Salesforce is optimizing its regional customer support hub to ensure minimal travel for field agents servicing client requests. Each client location receives a certain number of service visits per period. Locate the hub at an optimal location to minimize the total distance traveled by field agents and calculate the total distance required.
The hub is positioned at integral coordinates (hx, hy). Client location i is positioned at integral coordinates (x[i], y[i]). The distance to client location i is the Manhattan distance, defined as:
distance[i]=∣x[i]−hx∣+∣y[i]−hy∣
The total distance for client i is:
visits[i]×distance[i]
Determine the minimum total distance required for all service visits in a period.
Example x = [1, 3, 2, 4] y = [1, 2, 3, 4] visits = [1, 3, 2, 4] Function Description
Complete the function:
int optimalHubLocation(vector<int> visits,
vector<int> x,
vector<int> y)
Parameters int visits[n]: Number of service visits per period for each client location. int x[n]: The x coordinates of the client locations. int y[n]: The y coordinates of the client locations. Returns int: The minimum total distance for all service visits.

1≤n≤10^3
1≤x[i],y[i]≤10^4
1≤visits[i]≤50
Flipkart Grid 8.0 • Pending