> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/greedy/assign-mice-to-holes.md).

# Assign Mice to Holes

There are `N` Mice and `N` holes are placed in a straight line.\
Each hole can accomodate 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.\
Assign mice to holes so that the time when the last mouse gets inside a hole is minimized.

**Example:**

```
positions of mice are:
4 -4 2
positions of holes are:
4 0 5

Assign mouse at position x=4 to hole at position x=4 : Time taken is 0 minutes 
Assign mouse at position x=-4 to hole at position x=0 : Time taken is 4 minutes 
Assign mouse at position x=2 to hole at position x=5 : Time taken is 3 minutes 
After 4 minutes all of the mice are in the holes.

Since, there is no combination possible where the last mouse's time is less than 4, 
answer = 4.
```

**Input:**

```
A :  list of positions of mice
B :  list of positions of holes
```

**Output:**

```
single integer value
```

> **NOTE:** The final answer will fit in a 32 bit signed integer.

```java
public class Solution {
    public int mice(int[] A, int[] B) {
        Arrays.sort(A);
        Arrays.sort(B);
        int ans = Integer.MIN_VALUE;
        for (int i = 0; i < A.length; i++)
            ans = Math.max(ans, Math.abs(A[i] - B[i]));
        return ans;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/greedy/assign-mice-to-holes.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
