> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/coding_practice_questions/interview_questions_4/sorted-matrix-search.md).

# Sorted Matrix Search

**10.9 Sorted Matrix Search:** Given an M x N matrix in which each row and each column is sorted in ascending order, write a method to find an element.

* Approach:
  * The big idea in this problem was recognizing that a 2d array can be read as a single dimension array and vise-versa. A sorted matrix down the diagonal also implies that we can aline each row next to each other sequentially and have a single sorted array.

```cpp
struct MyException : public exception {
    const char* what() const throw() {
        return "Vector was empty";
    }
};

pair<int, int> d1_to_d2(int index_d1, int dim) {
    int row = index_d1 / dim;
    int col = index_d1 % dim;
    return make_pair(row, col);
}

pair<int, int> matrix_search(vector<vector<int>> &nxnMatrix, int find, int low, int high) {
    if (high > low)
    {
        int middle = low + ((high - low) / 2);
        pair<int, int> d2 = d1_to_d2(middle, nxnMatrix.size());

        if (nxnMatrix.at(d2.first).at(d2.second) == find) {
            return d2;
        }
        else if (nxnMatrix.at(d2.first).at(d2.second) > find) {
            //search left
            return matrix_search(nxnMatrix, find, low, middle - 1);
        }
        else {
            //search right
            return matrix_search(nxnMatrix, find, middle + 1, high);
        }
    }
    return make_pair(INT_MIN, INT_MIN);
}

pair<int, int> matrix_search(vector<vector<int>> &nxnMatrix, int find) {
    if (nxnMatrix.empty()) {
        throw MyException();
    }
    return matrix_search(nxnMatrix, find, 0, (nxnMatrix.size() * nxnMatrix.at(0).size()) - 1);
}

int main() {
    vector<vector<int>> matrix = {
        { 1, 2, 3, 4, 5, 6  },
        { 7, 8, 9, 10,11,12 },
        { 13,14,15,16,17,18 },
        { 19,20,21,22,23,24 },
        { 25,26,27,28,29,30 },
        { 31,32,33,34,35,36 }
    };

    pair<int, int> loc = matrix_search(matrix, 30);
    cout << loc.first << " " << loc.second << endl;
    pause();
}
```


---

# 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://maksimdan.gitbook.io/interview-practice-problems/coding_practice_questions/interview_questions_4/sorted-matrix-search.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.
