forked from kavyanshpandey/hacktoberfest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrixsearch.cpp
45 lines (41 loc) · 1.15 KB
/
matrixsearch.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
// given a matrix . write an algo to find that the given value exsists in the matrix or not . integers in each row are sorted in ascending order from left to right . integers in column are sorted in ascending fromn top to bottom.
//given a sq matrix a and its number of rows/columns return the transpose of A . the transpose matrix is the matrix flippped over its main diagonal , switching the row and column indices of matrix.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, m;
cin >> n >> m;
int key;
cin >> key;
int arr[n][m];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> arr[i][j];
}
}
int r=0,c=m-1;
bool flag=false;
while (r<n && c>=0){
if (arr[r][c] == key)
{
flag=true;
break;
}
else if (arr[r][c] > key)
{
c--;
}
else{
r++;
}
}
if(flag){
cout<<"element found"<<r<<c<<" ";}
else{
cout<<"not found";
}
return 0;
}