Minimal distance such that for every customer there is at least one vendor at given distance
Last Updated :
19 Jan, 2022
Given N and M number of points on the straight line, denoting the positions of the customers and vendors respectively. Each vendor provides service to all customers, which are located at a distance that is no more than R from the vendor. The task is to find minimum R such that for each customer there is at least one vendor at the distance which is no more than R.
Examples:
Input: N = 3, M = 2, customer[] = {-2, 2, 4}, vendor[] = {-3, 0}
Output: 4
Explanation: 4 is the minimum distance such that every customer is given service by at least one vendor.
Input: N = 5, M = 3, customer [] = {1, 5, 10, 14, 17}, vendor[] = {4, 11, 15}
Output: 3
Explanation: 3 is the minimum distance such that every customer is given service by at least one vendor.
Approach: This problem can be solved by using the Two Pointer approach. Follow the steps below to solve the given problem.
- Take two pointers, i for the customer array and j for the vendor array.
- Start moving the i pointer and for every customer i move the j index while customer[i] > vendor[j].
- Now when vendor[j] >= customer[i],
- so check the right distance between them which is vendor[j] – customer[i] if j < m.
- Check the left distance i.e. customer[i] – vendor[j – 1] if j > 0.
- Find the minimum out of these two i.e the shortest range that the customer[i] can be covered with; achieved by the comparison of the distance of those two adjacent vendors.
- Then maximize this property as much as possible to get the answer.
- Finally print the answer found.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int findMinDist( int customer[], int vendor[],
int N, int M)
{
int minR = 0;
int i = 0, j = 0;
while (i < N) {
if (j < M and vendor[j] < customer[i])
j++;
else {
int left_d = INT_MAX;
int right_d = INT_MAX;
if (j > 0)
left_d = customer[i]
- vendor[j - 1];
if (j < M)
right_d = vendor[j]
- customer[i];
int mn_d = min(left_d, right_d);
minR = max(minR, mn_d);
i++;
}
}
return minR;
}
int main()
{
int customer[] = { -2, 2, 4 };
int vendor[] = { -3, 0 };
int N = sizeof (customer)
/ sizeof (customer[0]);
int M = sizeof (vendor)
/ sizeof (vendor[0]);
cout << findMinDist(customer, vendor,
N, M);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int INT_MAX = 2147483647 ;
static int findMinDist( int customer[], int vendor[],
int N, int M)
{
int minR = 0 ;
int i = 0 , j = 0 ;
while (i < N) {
if (j < M && vendor[j] < customer[i])
j++;
else {
int left_d = INT_MAX;
int right_d = INT_MAX;
if (j > 0 )
left_d = customer[i] - vendor[j - 1 ];
if (j < M)
right_d = vendor[j] - customer[i];
int mn_d = Math.min(left_d, right_d);
minR =Math.max(minR, mn_d);
i++;
}
}
return minR;
}
public static void main(String[] args)
{
int customer[] = { - 2 , 2 , 4 };
int vendor[] = { - 3 , 0 };
int N = customer.length;
int M = vendor.length;
System.out.println(
findMinDist(customer, vendor, N, M));
}
}
|
Python3
INT_MAX = 2147483647
def findMinDist(customer, vendor, N, M):
minR = 0
i = 0
j = 0
while (i < N):
if (j < M and vendor[j] < customer[i]):
j + = 1
else :
left_d = 10 * * 9
right_d = 10 * * 9
if (j > 0 ):
left_d = customer[i] - vendor[j - 1 ]
if (j < M):
right_d = vendor[j] - customer[i]
mn_d = min (left_d, right_d)
minR = max (minR, mn_d)
i + = 1
return minR
customer = [ - 2 , 2 , 4 ]
vendor = [ - 3 , 0 ]
N = len (customer)
M = len (vendor)
print (findMinDist(customer, vendor, N, M))
|
Javascript
<script>
const INT_MAX = 2147483647;
const findMinDist = (customer, vendor, N, M) => {
let minR = 0;
let i = 0, j = 0;
while (i < N) {
if (j < M && vendor[j] < customer[i])
j++;
else {
let left_d = INT_MAX;
let right_d = INT_MAX;
if (j > 0)
left_d = customer[i]
- vendor[j - 1];
if (j < M)
right_d = vendor[j]
- customer[i];
let mn_d = Math.min(left_d, right_d);
minR = Math.max(minR, mn_d);
i++;
}
}
return minR;
}
let customer = [-2, 2, 4];
let vendor = [-3, 0];
let N = customer.length;
let M = vendor.length;
document.write(findMinDist(customer, vendor, N, M));
</script>
|
C#
using System;
class GFG
{
static int INT_MAX = 2147483647;
static int findMinDist( int []customer, int []vendor,
int N, int M)
{
int minR = 0;
int i = 0, j = 0;
while (i < N) {
if (j < M && vendor[j] < customer[i])
j++;
else {
int left_d = INT_MAX;
int right_d = INT_MAX;
if (j > 0)
left_d = customer[i] - vendor[j - 1];
if (j < M)
right_d = vendor[j] - customer[i];
int mn_d = Math.Min(left_d, right_d);
minR =Math.Max(minR, mn_d);
i++;
}
}
return minR;
}
public static void Main()
{
int []customer = { -2, 2, 4 };
int []vendor = { -3, 0 };
int N = customer.Length;
int M = vendor.Length;
Console.WriteLine(
findMinDist(customer, vendor, N, M));
}
}
|
Time Complexity: O(N + M)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...