class Solution { public: bool searchMatrix(vector<vector<int>>& matrix, int target) { int rows = matrix.size(); int cols = matrix[0].size(); int left = 0; int right = (rows * cols) - 1; // Treat the 2D matrix as a flat sorted array while (left <= right) { int mid = left + (right - left) / 2; int row = mid / cols; // convert flat index → row int col = mid % cols; // convert flat index → col int value = matrix[row][col]; if (value == target) { return true; // found! } if (value < target) { left = mid + 1; // target is to the right } else { right = mid - 1; // target is to the left } } return false; // not found } };