-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch in a Row-Column sorted matrix.cpp
58 lines (54 loc) · 1.16 KB
/
Search in a Row-Column sorted matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function template for C++
class Solution {
public:
bool matSearch(vector<vector<int>> &mat, int x) {
// your code here
int a=0,b=mat[0].size() - 1;
while(a<mat.size() && b>=0)
{
if(mat[a][b]==x)
{
return true;
}
else if(mat[a][b] > x)
{
b--;
}
else
{
a++;
}
}
return false;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int r, c;
cin >> r >> c;
vector<vector<int>> matrix(r, vector<int>(c, 0));
int x;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> matrix[i][j];
}
}
cin >> x;
Solution ob;
bool an = ob.matSearch(matrix, x);
if (an)
cout << "true" << endl;
else
cout << "false" << endl;
cout << "~" << endl;
}
return 0;
}
// } Driver Code Ends