What if you could find any number in a giant table in seconds, without checking every cell?
Why Search in 2D Matrix in DSA C?
Imagine you have a big table filled with numbers, like a spreadsheet. You want to find if a certain number is inside it. If you look at each number one by one, it will take a long time, especially if the table is very big.
Checking every number manually is slow and tiring. It's easy to make mistakes or miss a number. If the table is large, this method wastes a lot of time and effort.
Using a smart search method for a 2D matrix lets you find the number quickly without checking every single cell. It uses the order in the matrix to jump closer to the target, saving time and effort.
for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (matrix[i][j] == target) return true; } } return false;
int row = 0, col = cols - 1; while (row < rows && col >= 0) { if (matrix[row][col] == target) return true; else if (matrix[row][col] > target) col--; else row++; } return false;
This method makes searching in big tables fast and reliable, even when the data is huge.
Finding a specific product price in a sorted price list arranged by categories and price ranges, without checking every single price.
Manual search checks every cell and is slow.
Smart search uses matrix order to skip unnecessary checks.
Faster search saves time and reduces errors.
Practice
Solution
Step 1: Understand matrix sorting property
Each row is sorted and the first element of a row is greater than the last element of the previous row, so the entire matrix can be seen as a sorted list.Step 2: Apply binary search on the matrix
By treating the matrix as a single sorted array, binary search can be used efficiently to find the target.Final Answer:
Treat the 2D matrix as a sorted 1D array and apply binary search -> Option DQuick Check:
Matrix sorted row-wise = binary search [OK]
- Searching each row linearly wastes time
- Using hash map wastes extra space
- Starting from bottom-left only works if columns are sorted
Solution
Step 1: Calculate mid index in 1D
mid = (low + high) / 2 correctly finds the middle index between low and high.Step 2: Convert mid to 2D indices
Row is mid divided by number of columns (mid / cols), column is mid modulo number of columns (mid % cols).Final Answer:
int mid = (low + high) / 2; int row = mid / cols; int col = mid % cols; -> Option BQuick Check:
Row = mid/cols, Col = mid%cols [OK]
- Swapping rows and columns in calculation
- Using rows instead of cols for division/modulo
- Using bit shift incorrectly for mid
int matrix[3][4] = {
{1, 3, 5, 7},
{10, 11, 16, 20},
{23, 30, 34, 60}
};
int target = 5;
int rows = 3, cols = 4;
int low = 0, high = rows * cols - 1;
int found = 0;
while (low <= high) {
int mid = (low + high) / 2;
int r = mid / cols;
int c = mid % cols;
if (matrix[r][c] == target) {
found = 1;
break;
} else if (matrix[r][c] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
printf("%d\n", found);Solution
Step 1: Perform binary search on matrix
The code treats the matrix as a sorted 1D array and searches for target 5 using binary search.Step 2: Check if target 5 exists
5 is present at matrix[0][2], so found will be set to 1 and loop breaks.Final Answer:
1 -> Option AQuick Check:
Target found = 1 [OK]
- Confusing indices calculation for row and column
- Not updating low and high correctly
- Printing target value instead of found flag
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
int target = 4;
int rows = 2, cols = 3;
int low = 0, high = rows * cols - 1;
int found = 0;
while (low < high) {
int mid = (low + high) / 2;
int r = mid / cols;
int c = mid % cols;
if (matrix[r][c] == target) {
found = 1;
break;
} else if (matrix[r][c] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
printf("%d\n", found);Solution
Step 1: Analyze loop condition
The loop uses while (low < high), which misses the case when low == high, possibly skipping the last element.Step 2: Fix loop condition
Changing to while (low <= high) ensures all elements are checked, including when low equals high.Final Answer:
Change while condition to low <= high -> Option CQuick Check:
Loop must check low == high [OK]
- Using < instead of <= in loop condition
- Incorrect mid calculation causing infinite loop
- Swapping row and column indices
Solution
Step 1: Understand matrix property
Rows are sorted but no guarantee about relation between rows, so matrix is not fully sorted as 1D array.Step 2: Choose search method
Starting from top-right corner, move left if current > target, move down if current < target, efficiently narrows search.Final Answer:
Start from top-right corner and move left or down -> Option AQuick Check:
Partial row sorting = stair-step search [OK]
- Applying full binary search on unsorted matrix
- Searching each row linearly wasting time
- Using hash map wasting extra space
Ready to prove it?
Pass the test and this pattern moves unknown
