Skip to content

Commit

Permalink
search in 2d sorted matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
akshatnema committed Sep 23, 2021
1 parent 038b9b2 commit b405464
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions GFG/search_2dmatrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Question link - https://practice.geeksforgeeks.org/problems/search-in-a-matrix17201720/1

#include <bits/stdc++.h>
using namespace std;


// } Driver Code Ends
//User function template for C++
class Solution{
public:
int matSearch (vector <vector <int>> &mat, int N, int M, int X)
{
int i=0,j=M-1;
while(i<N || j>=0)
{
if(mat[i][j]==X)
return 1;
else if(mat[i][j]>X){
if(j>0)
j--;
else
return 0;
}
else{
if(i<N-1)
i++;
else
return 0;
}
}

return 0;
}
};

// { Driver Code Starts.

int main ()
{
int t; cin >> t;
while (t--){
int n, m; cin >> n >> m;
vector <vector <int>> arr (n, vector <int> (m));
for (int i=0;i<n;i++)
for (int j=0;j<m;j++)
cin >> arr[i][j];

int x; cin >> x;
Solution ob;
cout << ob.matSearch (arr, n, m, x) << endl;
}
} // } Driver Code Ends

0 comments on commit b405464

Please sign in to comment.