Search⌘ K
AI Features

Search in a Matrix

Understand how to search for a key in a 2D matrix where each row and column is sorted. Discover an efficient O(m+n) time algorithm starting from the top right corner, improving beyond naive scanning or binary search per row. Learn the logic to traverse the matrix while minimizing time and space complexity. This lesson equips you with a practical approach to handle matrix search problems commonly encountered in coding interviews.

Statement

Given a 2D array where all elements in any individual row or column are sorted. Find the position of a given key in such a matrix. If the key is not present in the matrix, return (1,1)(-1, -1) ...