Thursday, 12 March 2026

BINARY SEARCH IN A 2D MATRIX(accePted 100%tc)


 


class Solution {

public:

bool searchMatrix(vector<vector<int>>& mat, int target) {
int n=mat.size();
int m=mat[0].size();
int start=0;
int end=(n*m)-1;
while(start<=end){
int mid=(start+end)/2;
int row=(mid/m);
int col=mid%m;
if(mat[row][col]==target){
return true;
}
if(mat[row][col]<target){
start=mid+1;
}else end=mid-1;
}
return false;
}
};

No comments:

Post a Comment

SDE floyd-warshall algorithm

 // User function template for C++ class Solution {   public:     void floydWarshall(vector<vector<int>> &dist) {         //...