Opening the reading…
Opening the reading…
BINARY SEARCH › SEARCH ON MATRIX
Open — the attempt gate is not wired up yet
This editorial is meant to unlock after you have run the problem at least once, with the worked solution behind one further deliberate click. That needs per-learner unlock state nothing stores today, so for now the whole article is open.
Try it yourself first →Each row is sorted, and every value in a row is smaller than the first value in the next row. Therefore, if you read the matrix from left to right across the first row, then continue at the first cell of the next row, the values form one sorted array. The two-dimensional shape is only how the data is stored; it does not change the order that binary search needs.
Imagine that flattened array has indices 0 to m * n - 1. A flat index mid belongs to row mid / n and column mid % n. The quotient counts how many complete rows come before the index, while the remainder gives the position inside its row. Binary search can then compare the value at that location and discard half of the remaining cells.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(log(m * n)) | The search interval contains m * n flat indices and is at least halved after every comparison. Each iteration performs constant-time division, remainder, indexing, and comparison, so no cell is revisited. |
| Space | O(1) extra | Only the two bounds, the midpoint, and its row and column are stored. The input matrix is not copied, and there is no returned data structure whose storage needs to be counted. |
#include <vector>
using namespace std;
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m = matrix.size();
int n = matrix[0].size();
int low = 0;
int high = m * n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
int row = mid / n;
int col = mid % n;
int value = matrix[row][col];
if (value == target) {
return true;
}
if (value < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return false;
}
};The important placement is the translation after mid is chosen and before the comparison. The search bounds describe flat positions, while matrix access requires two coordinates, so row = mid / n and col = mid % n keep those two views synchronized. The updates use mid + 1 and mid - 1 because mid has already been tested and cannot remain in the unexplored interval.
You can first binary-search the row whose range could contain target, then binary-search inside that row. Specifically, choose the last row whose first value is no greater than target, and verify that target is no greater than that row's last value before searching within it. This is a different arrangement, not an optimisation: both stages together take O(log m + log n), which is O(log(m * n)) up to constant-factor reasoning, and both use O(1) extra space.
#include <vector>
using namespace std;
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m = matrix.size();
int n = matrix[0].size();
int low = 0;
int high = m - 1;
int row = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (matrix[mid][0] <= target) {
row = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
if (row == -1 || matrix[row][n - 1] < target) {
return false;
}
low = 0;
high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (matrix[row][mid] == target) {
return true;
}
if (matrix[row][mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return false;
}
};