-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch_in_a_row_col_sorted_matrix.cpp
More file actions
54 lines (44 loc) · 1.11 KB
/
Search_in_a_row_col_sorted_matrix.cpp
File metadata and controls
54 lines (44 loc) · 1.11 KB
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
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
// Function to search a given number in row-column sorted matrix.
bool search(vector<vector<int> > &matrix, int n, int m, int x) {
int row = 0;
int col = m - 1;
while(row < n && col >= 0)
{
if(matrix[row][col] == x) return true;
else if(matrix[row][col] < x) row += 1;
else col -= 1;
}
return false;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
vector<vector<int> > matrix(n);
for (int i = 0; i < n; i++) {
matrix[i].assign(m, 0);
for (int j = 0; j < m; j++) {
cin >> matrix[i][j];
}
}
int x;
cin >> x;
Solution obj;
if (obj.search(matrix, n, m, x))
cout << "1\n";
else
cout << "0\n";
}
return 0;
}
// } Driver Code Ends