Statement We have to search for a value in a sorted matrix . If exists, then return its coordinates , else return . x M x (i, j) (-1, -1) Let us consider the above matrix as an example. In this example, we are going to search for the value Since 12 is present in the matrix, the algorithm should return its coordinates ) 12. (2, 1 Simple Approach A simple approach is to traverse all the values in the matrix and check if it is equal to 12. The worst case time complexity of the above algorithm will be when Time Complexity O(n x m) = O(n²) n = m Efficient Approach The above algorithm behaves worse for large values of n and m. Let us look into the efficient algorithm now. Algorithm Start from Top Right position ( in the matrix 0, m - 1) M If the value is equal to return x (0, m - 1) Move one row down if the current value is less than x Move one column left if the current value is greater that x Let us apply this algorithm into our matrix We are going to search for the value in M. 12 M 1. Start from the Top Right value at is less than 12, so 12 should be somewhere in the bottom of the matrix since all row and column values are sorted in ascending order. So we move one row down. 5 M [0][4]. 5 2. The value at is also less than 12, so we move one row down 10 M [1][4] 3. The value at is greater than 12, so 12 should be somewhere in the left of the matrix, so we move one column left. 15 M [2][4] 4. The value at is also greater than 12, so we move one column left 14 M [2][3] 5. The value at is greater than 12, so we move one column left 13 M [2][2] 6. The value at is equal to 12, so we return its index M [2][1] (2, 1) Time Complexity The worst case time complexity of the above algorithm will be when O(n + m) = O(2n) = O(n) n = m, because we will be iterating all rows and all columns once if x is at the bottom left position (n - 1, 0) You can find the Java solution for this algorithm in the Github repo. https://github.com/ganeshkumarm1/DSAlgo/tree/master/src/com/dsalgo/Algorithms/BinarySearch2D Thank you 🙏 👋🤘 | Linkedin Github