> For the complete documentation index, see [llms.txt](https://docs-57.gitbook.io/data-structure-and-algorithms/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs-57.gitbook.io/data-structure-and-algorithms/problems/matrix/search-a-2d-matrix-ii.md).

# Search a 2D Matrix II

Write an efficient algorithm that searches for a value target in an m x n integer matrix matrix. This matrix has the following properties:

* Integers in each row are sorted in ascending from left to right.
* Integers in each column are sorted in ascending from top to bottom.

### Solutions

```csharp
public class Solution {
    public bool SearchMatrix(int[][] matrix, int target) {
        
        int row=0;
        int col=matrix[0].Length-1;
        
        while(row<matrix.Length && col>=0){
            
            if(matrix[row][col]==target){
                return true;
                
            }
            
            if(matrix[row][col]>target){
                col--;
            }
            else{
                row++;
            }
        }
        return false;
    }
}
```
