-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathMatrix Search
More file actions
36 lines (34 loc) · 731 Bytes
/
Matrix Search
File metadata and controls
36 lines (34 loc) · 731 Bytes
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
#include <iostream>
using namespace std;
void matrixSearch(int **arr, int n , int m, int key) {
for(int row = 0;row < n;) {
for(int col=m-1;col>=0 and row < n;) {
if(arr[row][col] == key) {
cout<<1;
return;
} else if(arr[row][col] > key) {
col--;
} else {
row++;
}
}
}
cout<<0;
return ;
}
int main() {
int n, m;
cin>>n>>m;
int **arr = new int*[n];
for(int i=0;i<n;i++) {
arr[i]=new int[m];
}
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
cin>>arr[i][j];
}
}
int key;
cin>>key;
matrixSearch(arr, n, m, key);
}