> 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/strings-arrays-and-2-pointers/min-steps-in-infinite-grid.md).

# Min Steps in Infinite Grid

You are in an infinite 2D grid where you can move in any of the 8 directions

```
 (x,y) to 
    (x+1, y), 
    (x - 1, y), 
    (x, y+1), 
    (x, y-1), 
    (x-1, y-1), 
    (x+1,y+1), 
    (x-1,y+1), 
    (x+1,y-1) 
```

You are given a sequence of points and **the order in which you need to cover the points.**. Give the minimum number of steps in which you can achieve it. You start from the first point.

**NOTE:** This question is intentionally left slightly vague. Clarify the question by trying out a few cases in the “See Expected Output” section.

\
\
**Input Format**<br>

Given two integer arrays A and B, where A\[i] is x coordinate and B\[i] is y coordinate of ith point respectively.\
\
**Output Format**<br>

Return an Integer, i.e minimum number of steps.\
\
**Example Input**<br>

Input 1:

```
 A = [0, 1, 1]
 B = [0, 1, 2]
```

\
**Example Output**\
Output 1:

```
 2
```

**Example Explanation**\
Explanation 1:

```
 Given three points are: (0, 0), (1, 1) and (1, 2).
 It takes 1 step to move from (0, 0) to (1, 1). It takes one more step to move from (1, 1) to (1, 2).
```

```java
public class Solution {
    public int coverPoints(ArrayList<Integer> A, ArrayList<Integer> B) {
        int steps=0;
        for(int i=0;i<A.size()-1;i++){
            int diffx=A.get(i+1)-A.get(i);
            int diffy=B.get(i+1)-B.get(i);
            if(diffx==0 || diffy==0){
                steps+=Math.abs(diffx)+Math.abs(diffy);
            }
            else if(Math.abs(diffx)==Math.abs(diffy)){
                steps+=Math.abs(diffx);
            }
            else{
                steps+=Math.min(Math.abs(diffx),Math.abs(diffy))+Math.abs(Math.abs(diffx)-Math.abs(diffy));
            }
        }
        return steps;
    }
}

```


---

# 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/strings-arrays-and-2-pointers/min-steps-in-infinite-grid.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.
