77. Search in a 2D Matrix

Given a 2-D array mat where the elements of each row are sorted in non-decreasing order, and the first element of a row is greater than the last element of the previous row (if it exists), and an integer target, determine if the target exists in the given mat or not.

Example 1:

Input: mat = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] ], target = 8

Output: True

Explanation: The target = 8 exists in the 'mat' at index (1, 3).

Example 2:

Input: mat = [ [1, 2, 4], [6, 7, 8], [9, 10, 34] ], target = 78

Output: False

Explanation: The target = 78 does not exist in the 'mat'. Therefore in the output, we see 'false'.

Now Your Turn!

Pick the correct output for the given input

Input: mat = [ [1, 2, 4], [6, 7, 8], [9, 10, 34] ], target = 7

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  •   n == mat.length
  •   m == mat[i].length
  •   1 <= m, n <= 100
  •   -104 <= mat[i][j], target <= 104

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution{
public:
bool searchMatrix(vector<vector<int>> &mat, int target){
}
};
Test Case

Input:

Mat
Target