What if you could find a number in a huge grid without looking at every single cell?
Why Search in 2D Matrix in DSA Typescript?
Imagine you have a big grid of numbers, like a spreadsheet, and you want to find if a certain number is inside it.
Without any special method, you look at each number one by one, moving row by row.
Checking every number takes a lot of time, especially if the grid is huge.
It's easy to get tired, make mistakes, or waste time looking at numbers that don't matter.
Using a smart search method, you can skip many numbers and jump closer to the target quickly.
This saves time and effort, making the search fast and easy.
for (let row = 0; row < matrix.length; row++) { for (let col = 0; col < matrix[0].length; col++) { if (matrix[row][col] === target) return true; } } return false;
let row = 0; let col = matrix[0].length - 1; while (row < matrix.length && col >= 0) { if (matrix[row][col] === target) return true; else if (matrix[row][col] > target) col--; else row++; } return false;
This method lets you quickly find if a number exists in a sorted grid without checking every single spot.
Think of looking for a name in a phone book sorted by last name and first name; you don't check every page but jump closer to the name by comparing.
Manual search checks every element, which is slow.
Smart search uses the sorted order to skip parts of the matrix.
This saves time and reduces mistakes.