DIY: Searching a 2D Matrix

Solve the interview question "Searching a 2D Matrix" in this lesson.

Problem statement

Given an m x n integer matrix and a target value, determine if the target exists in the matrix or not.

The matrix has the following properties:

  • Integers in each row are sorted from left to right.

  • The first integer of each row is greater than the last integer of the previous row.

Input

The input will be an m x n matrix of integers and a target integer value. The following is an example input:

matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]]

target = 30

Output

The output will be a Boolean value. The following is an example output for the above input:

True

The number 30 is present in the m x n matrix.

Coding exercise

Implement the search_matrix(matrix, target) function, where matrix is the m x n matrix of integers and target is the value that needs to be searched. The function will return a single Boolean value representing whether target exists in matrix or not.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.