11
How would you search for an element in a sorted matrix?
Tap to write answer
0 words | 0 charsPress Enter ↵ to reveal
Your Attempt
0 wordsRefined Model Answer
ReferenceI would first think about flattening the matrix and doing binary search, but that is only valid if the matrix ordering supports it. If each row and column is sorted, I would start from the top-right corner and move left or down depending on whether the current value is too large or too small. The reason I choose that approach is that it eliminates one row or one column at each step, giving O(m + n) time. If the matrix is globally sorted in row-major order, then I could use binary search for O(log(mn)) time. I would also mention edge cases like empty matrices and single-row or single-column inputs.