We are given two arrays mices[] and holes[] of same size. The mices[] represents the positions of the mice on a straight line, while holes[] represents the positions of the holes. Each hole can accommodate exactly one mouse. A mouse can either stay in its current position, move one step to the right, or move one step to the left, and each move takes one minute. We have to assign each mouse to a distinct hole in such a way that the time taken by the last mouse to reach its hole is minimized.
Examples:
Input : mices[] = [4, -4, 2] , holes[] = [4, 0, 5]
Output : 4
Explanation: Assign the mouse at position 4 to the hole at position 4, so the time taken is 0 minutes.
Assign the mouse at position −4 to the hole at position 0, so the time taken is 4 minutes.
Assign the mouse at position 2 to the hole at position 5, so the time taken is 3 minutes. Hence, the maximum time required by any mouse is 4 minutes
Input : mices[] = [1, 2], holes[] = [20, 10]
Output : 18
[Approach] Using Sorting - O(nlogn) Time and O(1) Space
The idea is to sort both arrays and then greedily assign each mouse to the corresponding hole in order. This ensures that every mouse is matched to its nearest possible hole, and the maximum time taken by any mouse is minimized.
Steps to solve the problem:
- Sort both arrays
micesandholes. - For each index
i(0 to n−1), compute the absolute difference|mices[i] − holes[i]|. - Keep track of the maximum difference encountered so far.
- That maximum value is the final answer.
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
int assignHole(vector<int>& mices, vector<int>& holes) {
int n = mices.size();
// Sort the arrays
sort(mices.begin(), mices.end());
sort(holes.begin(), holes.end());
// Finding max difference between
// ith mice and hole
int max = 0;
for(int i = 0; i < n; ++i)
{
if (max < abs(mices[i] - holes[i]))
max = abs(mices[i] - holes[i]);
}
return max;
}
int main()
{
vector<int> mices = { 4, -4, 2 };
vector<int> holes = { 4, 0, 5 };
// The required answer is returned
// from the function
int minTime = assignHole(mices, holes);
cout << minTime << endl;
return 0;
}
#include <stdio.h>
#include<stdlib.h>
int assignHole(int mices[], int holes[],
int n)
{
// Sort the arrays
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(mices[i]>mices[j]){
int temp=mices[i];
mices[i]=mices[j];
mices[j]=temp;
}
}
}
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(holes[i]>holes[j]){
int temp=holes[i];
holes[i]=holes[j];
holes[j]=temp;
}
}
}
// Finding max difference between
// ith mice and hole
int max = 0;
for(int i = 0; i < n; ++i)
{
if (max < abs(mices[i] - holes[i]))
max = abs(mices[i] - holes[i]);
}
return max;
}
int main()
{
int mices[] = { 4, -4, 2 };
int holes[] = { 4, 0, 5 };
int n = sizeof(mices) / sizeof(mices[0]);
// The required answer is returned
// from the function
int minTime = assignHole(mices, holes, n);
printf("%d",minTime);
return 0;
}
import java.util.Arrays;
public class GFG {
public static int assignHole(int[] mice, int[] holes) {
/* Sort the arrays */
Arrays.sort(mice);
Arrays.sort(holes);
int n = mice.length;
/* finding max difference between ith mice and hole */
int max = 0;
for (int i = 0; i <n; i++)
if (max < Math.abs(mice[i] - holes[i]))
max = Math.abs(mice[i] - holes[i]);
return Math.abs(max);
}
public static void main(String[] args) {
GFG gfg = new GFG();
int[] mice = {4, -4, 2};
int[] holes = {4, 0, 5};
System.out.println(gfg.assignHole(mice, holes));
}
}
def assignHole(mices, holes):
# Sort the arrays
mices.sort()
holes.sort()
n = len(mices)
# Finding max difference between
# ith mice and hole
Max = 0
for i in range(n):
if (Max < abs(mices[i] - holes[i])):
Max = abs(mices[i] - holes[i])
return Max
if __name__ == "__main__":
mices = [ 4, -4, 2 ]
holes = [ 4, 0, 5 ]
# The required answer is returned
# from the function
minTime = assignHole(mices, holes)
print(minTime)
using System;
class GFG
{
static int assignHole(int[] mices, int[] holes)
{
// Sort the arrays
Array.Sort(mices);
Array.Sort(holes);
int n = mices.Length;
// Finding max difference between
// ith mice and hole
int max = 0;
for(int i = 0; i < n; ++i)
{
if (max < Math.Abs(mices[i] - holes[i]))
max = Math.Abs(mices[i] - holes[i]);
}
return max;
}
static void Main()
{
int[] mices = { 4, -4, 2 };
int[] holes = { 4, 0, 5 };
// The required answer is returned
// from the function
int minTime = assignHole(mices, holes);
Console.WriteLine( minTime);
}
}
function assignHole(mices, holes) {
// Sort the arrays
mices.sort((a, b) => a - b);
holes.sort((a, b) => a - b);
let n = mices.length;
// Finding max difference between
// ith mice and hole
let max = 0;
for(let i = 0; i < n; ++i)
{
if (max < Math.abs(mices[i] - holes[i]))
max = Math.abs(mices[i] - holes[i]);
}
return max;
}
// Driver Code
let mices = [ 4, -4, 2 ];
let holes = [ 4, 0, 5 ];
// The required answer is returned
// from the function
let minTime = assignHole(mices, holes);
console.log(minTime);
Output
4