Search a 2D Matrix

PHOTO EMBED

Wed Jun 19 2024 06:32:24 GMT+0000 (Coordinated Universal Time)

Saved by @Xeno_SSY #c++

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        int s=0,e=(matrix.size()*matrix[0].size())-1;
        while(s<=e){
            int n=matrix[0].size();
            int mid=s+(e-s)/2;
            int i=mid/n;
            int j=(mid%n);
            if(matrix[i][j]==target){
                return true;
            }
            else if( matrix[i][j]<target) s=mid+1;
            else e=mid-1;
        }
        return false;
    }
};
content_copyCOPY

when matrix in row wise sorted and first element of a row in greater than the last element of last row