### Looking for good programming challenges?

Use the search below to find our solutions for selected questions!

# Given a matrix in which each row and each column is sorted, write a method to find an element in it.

Sharing is caring!

Problem statement
Given a matrix in which each row and each column is sorted, write a method to find an element in it.

Solution
Assumptions:
1. Rows are sorted left to right in ascending order. Columns are sorted top to bottom in ascending order.
2. Matrix is of size $n \times m$.

This algorithm works by elimination. Every move to the left (–col) eliminates all the elements below the current cell in that column. Likewise, every move down eliminates all the elements to the left of the cell in that row.

Code