> For the complete documentation index, see [llms.txt](https://zedive.gitbook.io/project-l/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zedive.gitbook.io/project-l/part-2/divide_and_conquer/binary_search/search-a-2d-matrix-ii.md).

# Search a 2D Matrix II

## Question ([LC.240](https://leetcode.com/problems/search-a-2d-matrix-ii/?tab=Description))

> Given a m x n matrix, find a target number. Each row is sorted in ascending order from left to right. Each column is also sorted in ascending order from top to bottom.

## Example

```
[
  [1,   4,  7, 11, 15],
  [2,   5,  8, 12, 19],
  [3,   6,  9, 16, 22],
  [10, 13, 14, 17, 24],
  [18, 21, 23, 26, 30]
]
target = 5, return true
target = 20, return false
```

## Analysis

This is different from each row is sorted and the start of next row is bigger than the end of the current row. We can't prune half of the matrix in this case. A `O(logn)` solution doesn't exist for this question.

## Approach

So what we can do instead is pruning on a row or a column base on the information we have. Start from the lower left corner (or upper right).

```
x is col, y is row
start from matrix[m][0]
while x < matrix[0].length and y >= 0
    if target > matrix[y][x]
        x++
    elif target < matrix[y][x]
        y--;
    else
        return true
end while
```

## Code

```java
public boolean searchMatrix(int[][] matrix, int target) {
    if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
        return false;
    }
    int x = 0, y = matrix.length - 1;    
    while (x < matrix[0].length && y >= 0) {
        if (target > matrix[y][x]) {
            x++;
        } else if (target < matrix[y][x]) {
            y--;
        } else {
            return true;
        }
    }
    return false;
}
```

## Time Complexity

O(m + n)


---

# 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://zedive.gitbook.io/project-l/part-2/divide_and_conquer/binary_search/search-a-2d-matrix-ii.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.
