question : Write an efficient algorithm that searches for a value target in an m x n integer matrix matrix. This matrix has the following properties:
Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom.
Ans : the solution given is correct but when element variable is updated at different location it gives error and I can't understand why?
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int trow = matrix.size();
int tcol = matrix[0].size();
int rowIndex = 0;
int colIndex = tcol - 1;
int element = matrix[rowIndex][colIndex]; // already defined here
while(rowIndex < trow && colIndex >= 0){
element = matrix[rowIndex][colIndex]; // works here
if(element == target){
return 1;
}
if(element > target){
colIndex --;
}
if(element < target){
rowIndex ++;
}
// element = matrix[rowIndex][colIndex]; // this gives error here
}
return 0;
}
};