Geeksforgeeks

Timer is Running

Given, **N** Mice and N holes are placed in a straight line. Each hole can accommodate only 1 mouse. A mouse can stay at his position, move one step right from **x to x + 1**, or move one step left from **x to x -1**. Any of these moves consumes **1** minute. Write a program to assign mice to holes so that the time when the last mouse gets inside a hole is minimized.

**Note: **Arrays M and H are the positions of the N mice and holes.

**Example 1:**

Input:N =3M ={4, -4, 2}H ={4, 0, 5}Output:4Explanation:If we assign mouse at 1st index to the hole at 1st, mouse at 2nd index to the hole at 2nd and the third to the hole at third. Then, the maximum time taken will be by the 2nd mouse, i.e. 4-0 = 4 minutes.

**Example 2:**

Input:N =2M ={4, 2}H ={1, 7}Output:3Explanation:If we assign mouse at 1st index to the hole at 2nd, and mouse at 2nd index to the hole at 1st, the maximum time taken will be by the 2nd mouse, i.e. 7-4 = 3 minutes.

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function **assignMiceHoles()** which takes an Integer N and arrays M and H as input and returns the answer.

**Expected Time Complexity:** O(N*log(N))

**Expected Auxiliary Space:** O(1)

**Constraints:**

1 <= N <= 10^{5}

-10^{5 }<= M[i] , H[i] <= 10^{5}

Author: harsh.agarwal0

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

jainshubham52535 | 529 |

Pathak1899 | 436 |

yashdip123 | 380 |

kirtidee18 | 377 |

jots62 | 351 |

kirtidee18 | 1057 |

ykaushik | 752 |

aggarwalvatsal5 | 741 |

harbilassingh | 576 |

pmr_3689 | 570 |

blackshadows | 5456 |

Ibrahim Nash | 5450 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4827 |

Login to report an issue on this page.