-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP77MATRIX.cpp
More file actions
53 lines (41 loc) · 719 Bytes
/
P77MATRIX.cpp
File metadata and controls
53 lines (41 loc) · 719 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Matrix Search
#include<iostream>
using namespace std;
int main(){
int n,m;
cin>>n>>m;
int target;
cin>>target;
int mat[n][m];
for(int i=0; i<n; i++)
{
for(int j=0 ;j<m; j++)
{
cin >> mat[i][j];
}
}
int r=0,c=m-1; // top right position of matrix
bool found = false;
while (r<n && c>=0)
{
if (mat[r][c] == target)
{
found=true;
}
if (mat[r][c] > target)
{
c--;
}
else{
r++;
}
}
if (found)
{
cout<<"element found";
}
else{
cout<<"element not found";
}
return 0;
}